跨线程调用问题
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void GUIDelegate(string paramString);
BackgroundWorker bgw = new BackgroundWorker();
private void button1_Click(object sender, EventArgs e)
{
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.WorkerSupportsCancellation = true;
bgw.RunWorkerAsync();
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
while (!bgw.CancellationPending)
{
DoGUIUpdate((i++).ToString());
System.Threading.Thread.Sleep(1000);
}
}
int i = 0;
public void DoGUIUpdate(string paramString)
{
if (this.InvokeRequired)
{
GUIDelegate delegateMethod = new GUIDelegate(DoGUIUpdate);
this.Invoke(delegateMethod, new object[] { paramString });
}
else
{
listBox1.Items.Add("the count is:" + paramString);
}
}
}
}
我有点不太明白,backgroundworker算是建立了一个线程,这个时候去调用窗口上空间的线程,于是this.InvokeRequired等于true,就开始执行this.Invoke,但是他怎么执行到listBox1.Items.Add("the count is:" + paramString);这一行的,不是太明白,而且好像这样的程序没办法debug。
总结就是2个问题:
1.他怎么执行到listBox1.Items.Add("the count is:" + paramString);这一行的
2.这样的程序如何debug
...全文
相关推荐
浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题
下面小编就为大家分享一篇浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题,具有很好的参考价值,希望对大家有所帮助
C#跨线程调用控件的四种方式
C#非控件创建线程调用控件的四种使用方式,从而避免跨线程调用的异常
STA客户跨线程调用STA对象
STA客户跨线程调用STA对象,使用了marshal,消息循环。
C# 跨线程调用控件
C# 跨线程调用控件经常报错,提供一种标准的跨新城调用控件的方法
c#中跨线程调用windows控件
c#中跨线程调用windows控件 c#中跨线程调用 c#中跨线程调用UI控件 c#中跨线程调用UI c#中跨线程调用windows控件 c#中跨线程调用 c#中跨线程调用UI控件 c#中跨线程调用UI


