2011-02-04 11 views
1

私のウェブサイトでは、バックグラウンドでプロセスを実行するスレッドを使用しています。私はボタンクリックでスレッドを開始します。ASP.NETでのスレッドのタイムアウトの問題

私が直面する問題は、タイムアウトして停止するように見えるということです。基本的にデータベースの更新は停止します。

何が間違っている可能性がありますか?ここで

は私のコードです:

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(); 
    } 
} 

答えて

2

バックグラウンドスレッドが唯一限り、それを呼び出すスレッドが生きているとして生きることができます。 Webサーバーには一定の要求/応答ライフサイクルがあるため、バックグラウンドのプロセスはこの制限時間を超えることはできません。 Webサーバーでタイムアウトに達すると、サーバーは応答(タイムアウト)を生成し、クライアントにプッシュしてスレッドを停止します。このイベントはバックグラウンドスレッドを終了させるので、データベースの更新が途中であれば停止します。ファイルへの書き込みのような処理をしている場合、そのファイルは開いたままになり、ロックされ、不適切に書き込まれます(破損したファイルへのアクセスを回復するために再起動が必要です)。

+0

あなたはこれに対する解決策は何だと思いますか? – meetpd

+0

@meetpd - 私は特に私が試して働くことを保証したものはありません。これは私の仕事の課題の1つで達成しなければならない課題でもあり、私は現在の未確認のアイデアを共有することができます。私はすべてのロジックをコマンドラインパラメータを受け入れるコンソールまたはwinforms実行可能ファイルに配置し、この記事で説明した非同期メソッドを使用して実行します:http://www.scribd.com/doc/12733315/How-to -Execute-Command-in-C –