2012-03-16 20 views
0

我有一個應用程序使用BackgroundWorker播放列表中的文件。按停止按鈕後,正在運行的文件將停止運行,但不會停止播放列表中的下一個文件。如何停止BackgroundWorker並退出我的所有循環

在代碼中的變量是: playCount - 有多少文件我已經運行,numberOfLoops - 我是多麼想循環運行,循環的意思是在我的列表框(listBoxFiles)的文件,myClass.playCapture - 什麼在後臺運行。

while (playCount < numberOfLoops && bContinuePlay && ifContinue) //play the file 
{ 
    for (int i = 0; (i < listBoxFiles.Items.Count) && bContinuePlay && ifContinue; i++) 
    { 
     string path = (string)listBoxFiles.Items[i]; 
     myClass = new myClass(path, playSpeed); 
     myClass.evePacketProgress += new myClass.dlgPacketProgress(
      (progressCount) => 
      { 
       myClass._fileSelectedIndex = i; 
       bgWoSingle.ReportProgress(progressCount, myClass); 
      }); 

     if (selectedAdapter != null) 
     { 
      bContinuePlay = myClass.playCapture(selectedAdapter._packetDevice); //here the BackgroundWorker running 
     } 
    } 

    playCount++; 
} 

回答

0

看起來像你也許忘了設置bContinuePlay或ifContinue假在停止代碼。如果情況並非如此,請提供更多代碼。 (開始和結束進程的地方)

+0

ifContinue是我的問題...謝謝 – user1269592 2012-03-16 15:02:15

+0

我仍然建議從我的回答中查看CancelAsync方法,因爲這可以清理一些代碼。 – 2012-03-16 15:08:57

+0

當調用CancelAsync方法時它會刪除BackgroundWorker的運行?那位取消後的人應該打開的所有其他人應該如何? – user1269592 2012-03-16 15:13:34