[英]Backgroundworker not firing
public Main()
{
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//Do Work
}
private void Start()
{
backgroundWorker1.RunWorkerAsync(); //This works perfectly
//////////This isthe offending code////////////
try
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + "FileName.ini";
using (var stream = File.Create(path)) { }
File.WriteAllText(path, "Text to write");
file = txtFilePath.Text;
}
catch (Exception ex)
{
MessageBox.Show(
"Error saving configuration file.");
}
//////////////////////////////////////////
backgroundWorker1.RunWorkerAsync(); //If moved here it does not fire!
//Program contines here...
}
不知道可能是什么問題。
我在這里可以想到兩件事。
編輯1:
您可以嘗試以下嗎?
private void Start()
{
backgroundWorker1.RunWorkerAsync(); //This works perfectly
Thread.Sleep(10000);
...
...
// rest of your code.
嘗試添加以下行:
bool running = backgroundWorker1.IsBusy();
在RunWorkerAsync()
被調用之后。 並使用調試器或打印消息檢查其值。 這將指示線程的狀態-我懷疑它立即完成了工作(?)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.