namespace WindowsFormsApplication2
{
class Class1
{
public volatile bool shouldStop;
private Form1 form;
public Class1(Form1 form)
{
this.form = form;
}
public void Method1(object obj)
{
string s = obj as string;
form.AddMessage(s);
while (shouldStop == false)
{
Thread.Sleep(1000);//将当前线程阻塞,线程1休眠100毫秒
form.AddMessage("a"); }
form.AddMessage("\n线程Method1已经终止");
}
public void Method2(object obj)
{
while (shouldStop == false)
{
Thread.Sleep(1000);//将当前线程阻塞,线程2休眠100毫秒
form.AddMessage("b"); }
form.AddMessage("\n线程Method2已经终止");
}
}
}namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Thread thread1, thread2;
Class1 cls;
public Form1()
{
InitializeComponent();
cls = new Class1(this); } private void btnStart_Click(object sender, EventArgs e)
{ richTextBox1.Clear();
cls.shouldStop = false;
thread1 = new Thread(cls.Method1);
thread1.IsBackground = true;
thread2 = new Thread(cls.Method2);
thread2.IsBackground = true;
thread1.Start("a method start\n");
thread2.Start();
} private delegate void AddMessageDelegate(string message);
public void AddMessage(string s)
{
if (richTextBox1.InvokeRequired == true)
{
AddMessageDelegate d = AddMessage;
richTextBox1.Invoke(d, s);
}
else
{
richTextBox1.AppendText(s);
}
} private void btnStop_Click(object sender, EventArgs e)
{
cls.shouldStop = true; thread2.Join(2000); //总是先是线程2结束,然后线程1结束
thread1.Join(5000);
/*
* thread1.Join(0);//问题1:总是先是线程2结束,然后线程1结束,为什么?
* thread2.Join(0); //问题2:看了参数表是时间,这里参数为0是表示 立即么?和无参数有什么区别?
*/
}
}
}
{
class Class1
{
public volatile bool shouldStop;
private Form1 form;
public Class1(Form1 form)
{
this.form = form;
}
public void Method1(object obj)
{
string s = obj as string;
form.AddMessage(s);
while (shouldStop == false)
{
Thread.Sleep(1000);//将当前线程阻塞,线程1休眠100毫秒
form.AddMessage("a"); }
form.AddMessage("\n线程Method1已经终止");
}
public void Method2(object obj)
{
while (shouldStop == false)
{
Thread.Sleep(1000);//将当前线程阻塞,线程2休眠100毫秒
form.AddMessage("b"); }
form.AddMessage("\n线程Method2已经终止");
}
}
}namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Thread thread1, thread2;
Class1 cls;
public Form1()
{
InitializeComponent();
cls = new Class1(this); } private void btnStart_Click(object sender, EventArgs e)
{ richTextBox1.Clear();
cls.shouldStop = false;
thread1 = new Thread(cls.Method1);
thread1.IsBackground = true;
thread2 = new Thread(cls.Method2);
thread2.IsBackground = true;
thread1.Start("a method start\n");
thread2.Start();
} private delegate void AddMessageDelegate(string message);
public void AddMessage(string s)
{
if (richTextBox1.InvokeRequired == true)
{
AddMessageDelegate d = AddMessage;
richTextBox1.Invoke(d, s);
}
else
{
richTextBox1.AppendText(s);
}
} private void btnStop_Click(object sender, EventArgs e)
{
cls.shouldStop = true; thread2.Join(2000); //总是先是线程2结束,然后线程1结束
thread1.Join(5000);
/*
* thread1.Join(0);//问题1:总是先是线程2结束,然后线程1结束,为什么?
* thread2.Join(0); //问题2:看了参数表是时间,这里参数为0是表示 立即么?和无参数有什么区别?
*/
}
}
}
解决方案 »
- 有无人会输入法的制作,讲讲原理可以吗?
- byte 数组 校验和 checksum 16进制 C#
- 程序假死,但是功能可以用百思不得其解!!请大家帮忙40分
- 问了基础的,关于float除法结果问题
- 投票系统思路请教..
- XML出错的问题?
- 高手指点:关于事件参数的问题
- [讨论]C#能做什么?
- VS.net 2005在XP下安装时出错
- 头大了,设计期间,窗口编辑器里怎么选择一个控件上全部子控件
- 在winfrom程序里面 DataGridViewButtonColumn列 按键的背景色怎样修改
- 用友直接插入数据库dispatchlist和dispatchlists开单,在用友中审核时,全部正常显示,报换算率为空,求
我改成: thread2.Join(5000);
thread1.Join(2000);输出结果:
a method start
bababa
线程Method2已经终止
线程Method1已经终止
问题1,是我理解错了,因为当时在调试的时候发现总是线程2先结束,然后线程1结束,换了一台电脑转换,发现原来是随机的,不是固定的。所以,应该是不存在线程2一定先于线程1结束的。
问题2,是说,如果Trhead1.Join(0)的话就是执行Thread1结束后交出剩余时间片?我试了一下如果不加参数就是主线程无反应了。