跨线程调用问题

yunhaiC QQ654777694 yunhaiC QQ654777694 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

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

...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
yunhaiC QQ654777694
C# 111,129 成员 642,538 内容