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...
}
不确定问题是什么。
答案 0 :(得分:3)
我可以在这里想到两件事。
编辑1:
您可以尝试以下方法吗?
private void Start()
{
backgroundWorker1.RunWorkerAsync(); //This works perfectly
Thread.Sleep(10000);
...
...
// rest of your code.
答案 1 :(得分:0)
尝试添加此行:
bool running = backgroundWorker1.IsBusy();
在RunWorkerAsync()
被召唤之后。并使用调试器或打印消息检查其值。
这将指示线程的状态 - 我怀疑它立即完成了它的工作(?)