##############################在主线程里使用lock等待加锁的时候,为什么连其它线程也全被阻塞了? 主线程ThreadMain和从线程ThreadChild同时需要访问一个变量A,从线程ThreadChild首先给此变量A使用lock加锁,在从线程ThreadChild还没有放开对变量A的加锁的时候,主线程ThreadMain也开始使用lock试图对变量A加锁使用,为什么在主线程ThreadMain开始阻塞的时候,其它所有的线程(包括正在对变量A拥有加锁的从线程ThreadChild)都被阻塞了?能不能帮着解释一下!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面是代码,首先单击button1开始执行ChildThreadMethod,然后单击button2开始执行MainThreadMethod。 static void Main() { Application.Run(new Form1()); } delegate void delegateShowValueInForm (); private void ChildThreadMethod () { System.Threading.Monitor.Enter(_objectShareData); while (_objectShareData.iShareData<50) { System.Threading.Thread.Sleep(100); _objectShareData.iShareData++; ShowValueInForm(); } System.Threading.Monitor.Exit(_objectShareData); } private void ShowValueInForm() { if (this.InvokeRequired) { this.Invoke(new delegateShowValueInForm(ShowValueInForm)); } else { textBox1.Text=_objectShareData.iShareData.ToString(); } } private void button1_Click(object sender, System.EventArgs e) { threadChild = new System.Threading.Thread(new System.Threading.ThreadStart(ChildThreadMethod)); threadChild.Start(); } private void MainThreadMethod() { System.Threading.Monitor.Enter(_objectShareData); _objectShareData.iShareData=10000; System.Threading.Monitor.Exit(_objectShareData); MessageBox.Show(_objectShareData.iShareData.ToString()); } private void button2_Click(object sender, System.EventArgs e) { MainThreadMethod(); } } public class DataObject { public int iShareData=0; } 其它所有的线程(包括正在对变量A拥有加锁的从线程ThreadChild)都被阻塞了?你是怎么知道的?在调试的线程列表里? 你的子线程入口函数不对,已进入子线程就lock对象了,直到子线程结束才释放,修改你的子线程入口函数,把锁定区放到循环中,如下:while (_objectShareData.iShareData<50){System.Threading.Thread.Sleep(100);System.Threading.Monitor.Enter(_objectShareData);//Enter critical region_objectShareData.iShareData++;ShowValueInForm();System.Threading.Monitor.Exit(_objectShareData); //Leave critical region} 谢谢愚翁。按照你的写法确实可以了,不过为什么将Lock放在子线程入口处就会阻塞所有线程呢?按道理讲只有等待Lock同一对象(_objectShareData)的线程(本例子中是主线程MainThread)才会被阻塞,其它的所有线程应该都不会受影响才对阿?按照我最开始的写法,把Lock放在子线程入口处,在子线程的方法里添一行System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString());结果连这个都不被输出了,所以我觉得其它的线程都被阻塞了,包括正在拥有锁的线程。private void ChildThreadMethod () { System.Threading.Monitor.Enter(_objectShareData); while (_objectShareData.iShareData<50) { System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString()); System.Threading.Thread.Sleep(100); _objectShareData.iShareData++; ShowValueInForm(); } System.Threading.Monitor.Exit(_objectShareData); } 你如下写private void ChildThreadMethod (){System.Threading.Monitor.Enter(_objectShareData);//Enter critical regionwhile (_objectShareData.iShareData<50){System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString());System.Threading.Thread.Sleep(100);_objectShareData.iShareData++;ShowValueInForm();}System.Threading.Monitor.Exit(_objectShareData);//Leave critical region}你看了以上的注释,应该很明白了吧,我就不多说了。 c#读取xml问题。。很简单 求XenoCode详细使用方法 请问有没有在C#中,可以编辑的Date控件? 急急急,sql语句格式问题??? 使用linq时怎么获得服务器时间 (服务器控件)页面中有5个textbox框,分别为txt1,txt2 ……txt5 有没有办法通过循环分别给他们赋不同的值(变量) 一道不是很理解的练习题目 请帮我分析分析这段代码 C# 关于XML序列化问题 请问哪有文章可以介绍一下如何使用VS2005中的 Team System 妖怪的问题!!!! C#中如何在Oracle中创建表?????
{
Application.Run(new Form1());
} delegate void delegateShowValueInForm ();
private void ChildThreadMethod ()
{
System.Threading.Monitor.Enter(_objectShareData);
while (_objectShareData.iShareData<50)
{
System.Threading.Thread.Sleep(100);
_objectShareData.iShareData++;
ShowValueInForm();
}
System.Threading.Monitor.Exit(_objectShareData);
}
private void ShowValueInForm()
{
if (this.InvokeRequired)
{
this.Invoke(new delegateShowValueInForm(ShowValueInForm));
}
else
{
textBox1.Text=_objectShareData.iShareData.ToString();
}
} private void button1_Click(object sender, System.EventArgs e)
{
threadChild = new System.Threading.Thread(new System.Threading.ThreadStart(ChildThreadMethod));
threadChild.Start();
} private void MainThreadMethod()
{
System.Threading.Monitor.Enter(_objectShareData);
_objectShareData.iShareData=10000;
System.Threading.Monitor.Exit(_objectShareData);
MessageBox.Show(_objectShareData.iShareData.ToString());
} private void button2_Click(object sender, System.EventArgs e)
{
MainThreadMethod();
}
} public class DataObject
{
public int iShareData=0;
}
while (_objectShareData.iShareData<50)
{System.Threading.Thread.Sleep(100);System.Threading.Monitor.Enter(_objectShareData);//Enter critical region
_objectShareData.iShareData++;
ShowValueInForm();
System.Threading.Monitor.Exit(_objectShareData); //Leave critical region}
按照我最开始的写法,把Lock放在子线程入口处,在子线程的方法里添一行System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString());结果连这个都不被输出了,所以我觉得其它的线程都被阻塞了,包括正在拥有锁的线程。
private void ChildThreadMethod ()
{
System.Threading.Monitor.Enter(_objectShareData);
while (_objectShareData.iShareData<50)
{
System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString());
System.Threading.Thread.Sleep(100);
_objectShareData.iShareData++;
ShowValueInForm();
}
System.Threading.Monitor.Exit(_objectShareData);
}
private void ChildThreadMethod ()
{
System.Threading.Monitor.Enter(_objectShareData);//Enter critical region
while (_objectShareData.iShareData<50)
{
System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString());
System.Threading.Thread.Sleep(100);
_objectShareData.iShareData++;
ShowValueInForm();
}
System.Threading.Monitor.Exit(_objectShareData);//Leave critical region
}你看了以上的注释,应该很明白了吧,我就不多说了。