private object sync = new object(); delegate void SetTextCallback(string text); private void WorkThread()
{
this.ReadText();
this.WorkThread();//为了循环
} private void ReadText()
{
int a;
lock(sync)
{
a = Convert.ToInt32(textBox1.Text);
}
Thread.Sleep(1000);
a++;
lock(sync)
{
this.SetText(a.ToString());
}
} private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
这样一段代码用下面代码创建线程 Thread t = new Thread(new ThreadStart(WorkThread));
t.Start();
循环指向那段启动代码启动的貌似也只是一个线程.
怎样才能用一个多个线程公用一个 方法? 或者我的代码就不对?
{
this.ReadText();
this.WorkThread();//为了循环
} private void ReadText()
{
int a;
lock(sync)
{
a = Convert.ToInt32(textBox1.Text);
}
Thread.Sleep(1000);
a++;
lock(sync)
{
this.SetText(a.ToString());
}
} private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
这样一段代码用下面代码创建线程 Thread t = new Thread(new ThreadStart(WorkThread));
t.Start();
循环指向那段启动代码启动的貌似也只是一个线程.
怎样才能用一个多个线程公用一个 方法? 或者我的代码就不对?
解决方案 »
- 怎么实现从一个form弹出另一个form后,原来的form不能操作,只能对当前的操作呢?
- WINFORM 的 会的来 , 本人 有问题 请教 急急急急急急急急急急啊, (如何把一个程序里的文件导入到另一个解决方案进行编译,)
- 求 Crystal Reports 11 (水晶报表11 )的注册码
- Vsual c#.Net编译完的程序,在没有装visual studio的电脑上运行,要装个怎样的程序?
- asp.net新手问题
- 100分求书,C#.net
- 请问编译后的dll,xml,pdb分别是什么内容,各有什么用处?
- 关于C#的网络资源
- 散分!!!
- 請各大版主、微軟的專家幫幫忙啊。
- 关于ListView的鼠标单击事件 急……
- 问下大家除了c/c++,vb,还有那些语言是直接编译为二进制,不是通过虚拟机执行的呀?
Thread t[i]=new Thread(new ThreadStart(WorkThread));
t[i].Start();