【问题标题】:Time out problem with thread in ASP.NETASP.NET 中的线程超时问题
【发布时间】:2011-06-21 08:24:32
【问题描述】:

在我的网站中,我使用线程在后台执行进程。我在单击按钮时启动线程。

现在我面临的问题是它似乎超时并停止。基本上它会停止更新数据库。

可能出了什么问题?

这是我的代码:

 public static class BackgroundHelper 
 {
    private static readonly object _syncRoot = new object(); 
    private static readonly ManualResetEvent _event = new ManualResetEvent(false); 
    private static Thread _thread;
    public static bool Running 
    { 
        get; 
        private set;
    }
    public static void Start()
    {    
        lock (_syncRoot) 
        {
          if (Running)
                return; 
            Running = true;
            // Reset the event so we can use it to stop the thread.
            _event.Reset(); 
            // Star the background thread.
            _thread = new Thread(new ThreadStart(BackgroundProcess)); 
            _thread.Start();
        }
    }

    public static void Stop()   
    {
        lock (_syncRoot)
        {
            if (!Running)
                return;
            Running = false; 
            // Signal the thread to stop.
            _event.Set();
            // Wait for the thread to have stopped.
            _thread.Join(); 
            _thread = null;
        } 
    }   

    private static void BackgroundProcess()  
    { 
        int count = 0;
        DateTime date1 = new DateTime(2011, 2, 5);
        while (System.DateTime.Compare(System.DateTime.Now, date1) < 0)
        {
            downloadAndParse();  
            // Wait for the event to be set with a maximum of the timeout. The
            // timeout is used to pace the calls to downloadAndParse so that 
            // it not goes to 100% when there is nothing to download and parse.
            bool result = _event.WaitOne(TimeSpan.FromSeconds(45));
            // If the event was set, we're done processing.
           // if (result)
             //   break;
            count++; 
        }
    }

    private static void downloadAndParse()
    {
        NewHive.MyServ newServe = new NewHive.MyServ();
        NewHive.CsvDownload newService = new NewHive.CsvDownload();
        //NewHive.MyServ newServe = new NewHive.MyServ();
        string downloadSuccess = newService.CsvDownloader();
        if (downloadSuccess == "Success")
        {
            string parseSuccess = newService.CsvParser();

        }
        newServe.updateOthersInPosition();
    }
}

【问题讨论】:

    标签: .net asp.net multithreading .net-3.5


    【解决方案1】:

    后台线程只能在调用它的线程存活时存活。由于 Web 服务器具有有限的请求/响应生命周期,因此您的后台进程不能超过此时间限制。一旦在 web 服务器上达到超时,服务器将生成响应(超时),将其推送到客户端并停止线程。这个事件会杀死你的后台线程,所以如果它在数据库中更新它就会停止。如果您正在执行诸如写入文件之类的操作,它将使该文件处于打开、锁定和不正确写入状态(需要重新启动才能重新获得对损坏文件的访问权限)。

    【讨论】:

    • 那么您认为解决这个问题的方法是什么?
    • @meetpd - 我没有专门尝试过并保证可以工作的人。这也是我在一个工作任务中必须完成的任务,我可以分享我目前未经测试的想法。我计划将我的所有逻辑放入一个控制台或 winforms 可执行文件中,该可执行文件接受命令行参数,然后使用本文中概述的异步方法执行它:scribd.com/doc/12733315/How-to-Execute-Command-in-C
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多