正文

HttpWebRequest 超时问题2006-08-03 11:31:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/Csharpsky/17159.html

分享到:

我怎么判断是否超时?现在我的程序每次超时就抛出异常。
怎么让他超时以后等几分钟然后再尝试呢?


可以采用线程池,超时后调用回调函数
问题是 我不知道怎么判断他超时!
[C#]
using System;
using System.Threading;
// TaskInfo contains data that will be passed to the callback
// method.
public class TaskInfo {
    public RegisteredWaitHandle Handle = null;
    public string OtherInfo = "default";
}
public class Example {
    public static void Main(string[] args) {
        // The main thread uses AutoResetEvent to signal the
        // registered wait handle, which executes the callback
        // method.
        AutoResetEvent ev = new AutoResetEvent(false);
        TaskInfo ti = new TaskInfo();
        ti.OtherInfo = "First task";
        // The TaskInfo for the task includes the registered wait
        // handle returned by RegisterWaitForSingleObject.  This
        // allows the wait to be terminated when the object has
        // been signaled once (see WaitProc).
        ti.Handle = ThreadPool.RegisterWaitForSingleObject(
            ev,
            new WaitOrTimerCallback(WaitProc),
            ti,
            1000,
            false
        );
        // The main thread waits three seconds, to demonstrate the
        // time-outs on the queued thread, and then signals.
        Thread.Sleep(3100);
        Console.WriteLine("Main thread signals.");
        ev.Set();
        // The main thread sleeps, which should give the callback
        // method time to execute.  If you comment out this line, the
        // program usually ends before the ThreadPool thread can execute.
        Thread.Sleep(1000);
        // If you start a thread yourself, you can wait for it to end
        // by calling Thread.Join.  This option is not available with 
        // thread pool threads.
    }
   
    // The callback method executes when the registered wait times out,
    // or when the WaitHandle (in this case AutoResetEvent) is signaled.
    // WaitProc unregisters the WaitHandle the first time the event is 
    // signaled.
    public static void WaitProc(object state, bool timedOut) {
        // The state object must be cast to the correct type, because the
        // signature of the WaitOrTimerCallback delegate specifies type
        // Object.
        TaskInfo ti = (TaskInfo) state;
        string cause = "TIMED OUT";
        if (!timedOut) {
            cause = "SIGNALED";
            // If the callback method executes because the WaitHandle is
            // signaled, stop future execution of the callback method
            // by unregistering the WaitHandle.
            if (ti.Handle != null)
                ti.Handle.Unregister(null);
        } 
        Console.WriteLine("WaitProc( {0} ) executes on thread {1}; cause = {2}.",
            ti.OtherInfo, 
            Thread.CurrentThread.GetHashCode().ToString(), 
            cause
        );
    }
}

还有“ 采用线程池,超时后调用回调函数”
怎么用,这个我还没接触过

看看这例子,如果有异常抛出,继续执行线程,若数据已获取到,则关闭线程
有异常抛出就可能是超时
web config
<configuration>
   <system.web>
      <httpRuntime executionTimeout="??seconds">
   </system.web>
</configuration>
好象是改这个。
不过有一个问题,你的Web应用程序要执行这么长时间?????
建议你再看一看代码。

我的是winform
我今天才看你的代码
看不太懂,为什么RegisterWaitForSingleObject第三个参数必须是个对象呢?
我只需要在超时的时候重新执行一下一个函数而已!
上面的代码我实在是弄不懂呀!
线程池是干什么用的!没看明白

阅读(7546) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册