2012-10-12 47 views
0

更新C#後臺工作RunWorkerCompleted永遠不會觸發 - 爲什麼?

我現在想知道的事實是IEnumerable的在這裏當foreach循環迭代是從使用收益率回報循環。我不確定這對線程是否有影響...?


任何人都可以指出爲什麼我從來沒有看到這個代碼的BackgroundWorker RunWorkerCompleted事件火(這是一個.NET 4,MVC 3應用程序)?

不管有什麼事集,WorkerSupportsCancellationWorkerReportsProgress完成的事件似乎永遠不會開火。

即使當我試圖在DoWork的塊拋出一個異常,我從來沒有看到已完成的事件。據我瞭解,我應該。

任何明顯嗎?

順便說一句,我無法將項目升級到.NET 4.5使用較新的異步功能,由於項目的限制。

var autoResetEvent = new AutoResetEvent(false); 

UploadsExpected = pagesFound; 

foreach (var rasterisedPageFilePath in rasterisedPageFilePathList) 
{ 
    // Track uploads 

    UploadsStarted += 1; 

    int uploadCount = UploadsStarted; 

    // Track concurrent uploads in main thread and while we are at our 
    // maximum, pause and wait before starting the next upload thread 

    while (ConcurrentUploadsRunning >= maxConcurrentUploads) 
    { 
     Debug.WriteLine(string.Format("Waiting to start upload: {0}", 
      uploadCount)); 

     Thread.Sleep(3000); 
    } 

    ConcurrentUploadsRunning += 1; 

    Debug.WriteLine(string.Format("Initiating new upload: {0}", uploadCount)); 

    // Create a background worker so we can run the upload asynchronously. 

    var backgroundWorker = new BackgroundWorker(); 

    // Set up anonymous method that runs asynchronously 

    backgroundWorker.DoWork += (sender, e) => 
    { 
     try 
     { 
      var storageManager = new storageManager(awsS3BucketName); 

      string imgFilePath = (string) e.Argument; 

      using (var fileStream = new FileStream(imgFilePath, FileMode.Open)) 
      { 
       storageManager.Save(Path.GetFileName(imgFilePath), 
        MimeTypes.JPEG, fileStream); 
      } 
     } 
     catch (Exception ex) 
     { 
      UploadHasFailed = true; 

      m_logManager.Fatal("Upload of file to AWS S3 has failed", ex); 
     } 
     // Run check for AutoResetEvent following Save complete, 
     // and if the completed uploads count indicates that all uploads 
     // have finished, set AutoResetEvent so main thread can exit 

     if ((UploadsCompleted += 1) == UploadsExpected) 
     { 
      autoResetEvent.Set(); 
     } 

     Debug.WriteLine(string.Format("Upload complete: {0}", uploadCount)); 

     ConcurrentUploadsRunning -= 1; 
    }; 

    backgroundWorker.RunWorkerCompleted += (sender, args) => 
    { 
     // Never fires 
    }; 

    backgroundWorker.ProgressChanged += (sender, args) => 
    { 
     // Never fires 
    }; 

    backgroundWorker.RunWorkerAsync(rasterisedPageFilePath); 

} 

autoResetEvent.WaitOne(); 

try 
{ 
    inputStream.Close(); 

} catch { } 
+1

你打算使用'任務'?在這種情況下支持可能會容易得多,並且可以在.NET 4.0中使用。 – casperOne

+0

這可能是因爲您的文件流被阻止。你有權訪問該文件嗎? – Silvermind

+0

@Silvermind,是該文件的任何句柄已被關閉。 – gb2d

回答

-1

有這個代碼很多奇怪的怪癖 - 是執行你的主線程或其他線程上面的代碼的線程?無論哪種方式,如果它是你的GUI線程,然後它阻止(.Sleep(3000)),如果它不是,那麼你在錯誤的背景下創建你的背景工作

+0

睡眠是爲了控制後臺線程的產生,並且這不會阻塞後臺工作線程運行的線程。 – gb2d

+0

「很多奇怪的怪癖」......列出你認爲的那些內容會很有幫助。 –

+0

如果我們知道代碼在哪個線程中執行,那麼回答起來會更容易,目前尚不清楚。而且我不確定我是否明白代碼的目的是什麼。如果我知道代碼的上下文和目的,那麼怪癖列表更容易填充 – Onkelborg

相關問題