C# Winforms中的后台工作程序未更新文本框
我正试图使用后台工作程序向winform添加取消按钮 我已经添加了下面的语法,但是文本框从来没有像我尝试实现后台工作程序之前那样随着任何进展而更新C# Winforms中的后台工作程序未更新文本框,c#,winforms,asynchronous,backgroundworker,C#,Winforms,Asynchronous,Backgroundworker,我正试图使用后台工作程序向winform添加取消按钮 我已经添加了下面的语法,但是文本框从来没有像我尝试实现后台工作程序之前那样随着任何进展而更新 public Form1() { AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { string resourceName = new AssemblyName(args.Name).Name + ".dll"; stri
public Form1()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string resourceName = new AssemblyName(args.Name).Name + ".dll";
string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName));
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
}
private void btnQuery_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//Iterating the array
foreach (string s in sxc)
{
txt_ProgressDetails.Visible = true;
AppendTextBoxLine("Getting Data For " + s);
//Put data into DataTable
PopulateDT(s);
}
//If the cancel button was pressed
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
}
private void AppendTextBoxLine(string statusMessage)
{
if (txt_ProgressDetails.Text.Length > 0)
txt_ProgressDetails.AppendText(Environment.NewLine);
txt_ProgressDetails.AppendText(statusMessage);
}
BackgroundWorker的Do_Work事件在非STA线程中运行,您不能从那里更新UI。您可以重写代码,从使用
invokeRequested
和Invoke
方法创建UI元素的线程中更新UI元素
将以下内容添加到您的表单中:
delegate void UpdateUICallback(string statusMessage);
并将您的AppendTextBoxLine
方法更改为:
if (InvokeRequired)
{
UpdateUICallback d = new UpdateUICallback(AppendTextBoxLine);
this.Invoke(d, new object[] {statusMessage});
}
else
{
if (txt_ProgressDetails.Text.Length > 0)
txt_ProgressDetails.AppendText(Environment.NewLine);
txt_ProgressDetails.AppendText(statusMessage);
}
因此,您的代码将如下所示:
public Form1()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string resourceName = new AssemblyName(args.Name).Name + ".dll";
string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName));
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
}
private void btnQuery_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//Iterating the array
foreach (string s in sxc)
{
txt_ProgressDetails.Visible = true;
AppendTextBoxLine("Getting Data For " + s);
//Put data into DataTable
PopulateDT(s);
}
//If the cancel button was pressed
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
}
delegate void UpdateUICallback(string statusMessage);
private void AppendTextBoxLine(string statusMessage)
{
if (InvokeRequired)
{
UpdateUICallback d = new UpdateUICallback(AppendTextBoxLine);
this.Invoke(d, new object[] {statusMessage});
}
else
{
if (txt_ProgressDetails.Text.Length > 0)
txt_ProgressDetails.AppendText(Environment.NewLine);
txt_ProgressDetails.AppendText(statusMessage);
}
}
如果我添加代理语音UpdateUICallback();对于我的Form1(),我得到了多个编译错误为什么?什么编译错误?我编辑了答案,这样该方法就可以将一个字符串参数作为您以前的代码我编辑了答案以改进它,同时回答这个问题您的代码看起来有点古怪。循环后检查
if(backgroundWorker1.CancellationPending)
有什么意义?那就太晚了。