请高手帮忙解决VS.net2005多线程处理
private delegate void dodelegate(System.Windows.Forms.ProgressBar pb);private void button4_Click(object sender, EventArgs e)
{
dodelegate mydo = new dodelegate(doit);
mydo.BeginInvoke(progressBar1, null, null);
}
private void doit(System.Windows.Forms.ProgressBar pb)
{
pb.Minimum = 0;
pb.Maximum = 100;
pb.Step = 1;
pb.Value = 0;
for (int i = 0; i < 100; i++)
{
pb.Value = pb.Value + 1;
System.Threading.Thread.Sleep(200);
}
}
执行button4_click代码时,返回错误:
线程间操作无效: 从不是创建控件“pb”的线程访问它
请高手帮我解决一下,谢谢
--------------------编程问答-------------------- 线程间直接操作是无效,可以用一个代理
主线程:
第一步:定义委托delegate
private delegate void setConn(string strConn);
private setConn mySetConn = null;
第二步:定义一个方法
private void DispText(string strConn)
{
textBox1.Text = strConn;
}
第三步:初始化委托
mySetConn = new setConn(this.DispText);
第四步:在工作线程中
this.Invoke(mySetConn, "把我显示在textBox1中,谢谢!"); --------------------编程问答-------------------- 是阻塞的,我需要非阻塞的,要求像多线程的一样的效果 --------------------编程问答-------------------- private delegate void dodelegate(System.Windows.Forms.ProgressBar pb);
dodelegate mydo = null;
private void doit(System.Windows.Forms.ProgressBar pb)
{
if (progressBar1.InvokeRequired)
{
pb.Minimum = 0;
pb.Maximum = 100;
pb.Step = 1;
pb.Value = 0;
for (int i = 0; i < 100; i++)
{
pb.Value = pb.Value + 1;
System.Threading.Thread.Sleep(200);
}
}
}
private void button4_Click(object sender, EventArgs e)
{
mydo = new dodelegate(doit);
mydo.BeginInvoke(progressBar1,null,null);
}
progressbar1为一个窗体上的控件,但是在执行时,仍然报线程间操作无效: 从不是创建控件“progressBar1”的线程访问它,有高手能解答吗,谢谢!最好能贴出代码片断 --------------------编程问答-------------------- 线程中不能直接给控件赋值,需要调用控件的Invoke方法 --------------------编程问答-------------------- 先看看这篇文章,我回答部分
http://community.csdn.net/Expert/topic/5339/5339904.xml?temp=.8979608 --------------------编程问答-------------------- 请问高人上述代码如何改写才对,请指教 --------------------编程问答-------------------- up --------------------编程问答-------------------- 有没有人知道怎么改写上述代码,万分感谢。 --------------------编程问答-------------------- public partial class Form1 : Form
{
private delegate void dodelegate(System.Windows.Forms.ProgressBar pb);
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
dodelegate mydo = new dodelegate(doit);
this.Invoke(mydo, new object[] { progressBar1 });
}
private void doit(System.Windows.Forms.ProgressBar pb)
{
pb.Minimum = 0;
pb.Maximum = 100;
pb.Step = 1;
pb.Value = 0;
for (int i = 0; i < 100; i++)
{
pb.Value = pb.Value + 1;
System.Threading.Thread.Sleep(200);
}
}
}
不过这样写窗体会假死 --------------------编程问答-------------------- 用下面的方法就不会发生假死了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
Worker worker = new Worker();
worker.ProcessEvent += new ProcessEventHandler(worker_ProcessEvent);
worker.Start();
}
private void worker_ProcessEvent(int count)
{
this.Invoke(new ProcessEventHandler(this.ShowProcess), new object[] { count });
}
private void ShowProcess(int count)
{
progressBar1.Value = count;
}
}
public delegate void ProcessEventHandler(int count);
public class Worker
{
public event ProcessEventHandler ProcessEvent = null;
public void Start()
{
Thread thread = new Thread(new ThreadStart(delegate()
{
for (int i = 0; i < 100; i++)
{
if (ProcessEvent != null)
{
ProcessEvent(i);
}
Thread.Sleep(200);
}
}));
thread.IsBackground = true;
thread.Start();
}
}
}
补充:.NET技术 , C#