在Form1.cs中开始线程:
private void button1_Click(object sender, EventArgs e)
{
Thread[] th = new Thread[10];
for (int i = 0; i < ThreadNum; i++)
{
th[i] = new Thread(new ThreadStart(pro.RequestProc));
th[i].Start();
}
} public void showMsg(string msg)
{
MessageBox.Show("测试是否委托传入"+msg);
if (this.listBox1.InvokeRequired)
{
if (this.listBox1.Items.Count < 20)
{
this.listBox1.Items.Add(msg);
}
else
{
this.listBox1.Items.RemoveAt(0);
this.listBox1.Items.Add(msg);
}
}
}在Processer.cs中:
public delegate void MydeleGate(string msg);
public class Processer
{
public MydeleGate mydel;
Form1 oForm = new Form1();
public void RequestProc()
{
..................
string a = "haha";
mydel = new MydeleGate(oForm.showMsg);
mydel(a);
}
}我现在能够在showMsg里接收到msg的值,但是就是显示不出来!求帮忙!今天刚学委托,实在不行了!谢谢!30分!
private void button1_Click(object sender, EventArgs e)
{
Thread[] th = new Thread[10];
for (int i = 0; i < ThreadNum; i++)
{
th[i] = new Thread(new ThreadStart(pro.RequestProc));
th[i].Start();
}
} public void showMsg(string msg)
{
MessageBox.Show("测试是否委托传入"+msg);
if (this.listBox1.InvokeRequired)
{
if (this.listBox1.Items.Count < 20)
{
this.listBox1.Items.Add(msg);
}
else
{
this.listBox1.Items.RemoveAt(0);
this.listBox1.Items.Add(msg);
}
}
}在Processer.cs中:
public delegate void MydeleGate(string msg);
public class Processer
{
public MydeleGate mydel;
Form1 oForm = new Form1();
public void RequestProc()
{
..................
string a = "haha";
mydel = new MydeleGate(oForm.showMsg);
mydel(a);
}
}我现在能够在showMsg里接收到msg的值,但是就是显示不出来!求帮忙!今天刚学委托,实在不行了!谢谢!30分!
private void button1_Click(object sender, System.EventArgs e)
{
Processer pro=new Processer(this);
Thread[] th = new Thread[5];
for (int i = 0; i < th.GetLongLength(0); i++)
{
th[i] = new Thread(new ThreadStart(pro.RequestProc));
th[i].Start();
}
}
public void showMsg(string msg)
{
MessageBox.Show("测试是否委托传入"+msg);
if (this.listBox1.InvokeRequired)
{
if (this.listBox1.Items.Count < 20)
{
this.listBox1.Items.Add(msg);
}
else
{
this.listBox1.Items.RemoveAt(0);
this.listBox1.Items.Add(msg);
}
}
//主要是这里
public class Processer
{
public delegate void MydeleGate(string msg);
public Form1 frm;
public MydeleGate mydel;
public Processer(Form1 f)
{
frm=f;
}
public void RequestProc()
{
string a = "haha";
mydel = new MydeleGate(frm.showMsg);
mydel(a);
}
}
public void showMsg(string msg)
{
// MessageBox.Show("测试是否委托传入" + msg);
if (this.listBox1.InvokeRequired)
{
MydeleGate d = new MydeleGate(showMsg);
this.Invoke(d, new object[] { msg });
}
else
{ if (this.listBox1.Items.Count < 20)
{
this.listBox1.Items.Add(msg);
}
else
{
this.listBox1.Items.RemoveAt(0);
this.listBox1.Items.Add(msg);
}
}
}
线程间操作无效: 从不是创建控件“listBox1”的线程访问它。
不过把
public delegate void MydeleGate(string msg);放在了
public class Processer上面加上jointan() 老兄的已经成功了!~谢谢两位!~不过这个原理还不是很懂!!一定要在构造的时候加进去吗?
Processer pro = new Processer();
pro.oForm = this;
Thread[] th = new Thread[10];
for (int i = 0; i < 10; i++)
{
th[i] = new Thread(new ThreadStart(pro.RequestProc));
th[i].Start();
}
/////////////////////////////////////////////////////
public class Processer
{
public MydeleGate mydel;
public Form1 oForm = null;这句我临时改成了public
public void RequestProc()
{
// ..................
string a = "haha";
mydel = new MydeleGate(oForm.showMsg);
mydel(a);
}
}
一但InvokeRequired为真的话,说明执行方法的线程不属于创建控件的线程,那么就需要把这个方法"外包"给一个使用Invoike的执行步骤去执行,而且按Invoike的规定,必须把要执行的函数包装成一个委托.