积极答复者
更改Thread.Name属性时候出现的无法更改问题,求助额~~~~

问题
-
先贴错误提示再贴代码
“System.InvalidOperationException”类型的未经处理的异常出现在 mscorlib.dll 中。
调试中断的地方为thread1.Name = "";和thread2.Name = "";的地方
其他信息: 该属性已经设置,不能修改。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace _2._1_2._2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } public void btn1_change() { int i = 0; for(; i < 10; i++) { Thread thread1 = Thread.CurrentThread; thread1.Name = "btn1_change"; label1.Text = String.Format("{0},{1}", i, thread1.Name); label1.Refresh(); Thread.Sleep(500); } } public void btn2_change() { int i = 0; for(; i < 10; i++) { Thread thread2 = Thread.CurrentThread; thread2.Name = "btn2_change"; label2.Text = String.Format("{0},{1}", i, thread2.Name); label2.Refresh(); Thread.Sleep(500); } } private void button1_Click(object sender, EventArgs e) { Thread thread1 = new Thread(new ThreadStart(btn1_change)); thread1.Start(); Thread thread2 = new Thread(new ThreadStart(btn2_change)); thread2.Start(); } } }
vortex.piggy
- 已编辑 Vortex.piggy 2012年2月12日 9:16
答案
-
简单的说,threa.Name 是个write-once property,所以你第一次赋值以后就不能再次修改它了,,至于为什么这么设计的原因你可以参考下面的2个URL
http://social.msdn.microsoft.com/Forums/en-SG/netfxbcl/thread/c59593fb-4961-4eb9-9321-f165d96fa9ef
http://blogs.msdn.com/b/bclteam/archive/2003/11/11/49736.aspx
- 已编辑 Jacky_shen 2012年2月12日 12:51
- 已标记为答案 Vortex.piggy 2012年2月12日 13:44
-
“其他信息: 该属性已经设置,不能修改” 提示信息不是写的很清楚了嘛,这肯定是你命名过一次后的再次赋值。
- 已标记为答案 Vortex.piggy 2012年2月13日 7:38
-
请改为如下的代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Threading;
namespace_2._1_2._2
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls=false;
}
publicvoidbtn1_change()
{
int i =0; thread1.Name="btn1_change";
for(;i <10;i++)
{
Threadthread1 =Thread.CurrentThread;
label1.Text=String.Format("{0},{1}",i, thread1.Name);
label1.Refresh();
Thread.Sleep(500);
}
}
publicvoidbtn2_change()
{
int i =0; thread2.Name="btn2_change";
for(;i <10;i++)
{
Threadthread2 =Thread.CurrentThread;
label2.Text=String.Format("{0},{1}",i,thread2.Name);
label2.Refresh();
Thread.Sleep(500);
}
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
Threadthread1 =newThread(newThreadStart(btn1_change));thread1.Start();
Threadthread2 =newThread(newThreadStart(btn2_change));
thread2.Start();
}
}
}
- 已标记为答案 Vortex.piggy 2012年2月12日 13:44
- 已编辑 Jacky_shen 2012年2月12日 13:48
全部回复
-
简单的说,threa.Name 是个write-once property,所以你第一次赋值以后就不能再次修改它了,,至于为什么这么设计的原因你可以参考下面的2个URL
http://social.msdn.microsoft.com/Forums/en-SG/netfxbcl/thread/c59593fb-4961-4eb9-9321-f165d96fa9ef
http://blogs.msdn.com/b/bclteam/archive/2003/11/11/49736.aspx
- 已编辑 Jacky_shen 2012年2月12日 12:51
- 已标记为答案 Vortex.piggy 2012年2月12日 13:44
-
“其他信息: 该属性已经设置,不能修改” 提示信息不是写的很清楚了嘛,这肯定是你命名过一次后的再次赋值。
- 已标记为答案 Vortex.piggy 2012年2月13日 7:38
-
请改为如下的代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Threading;
namespace_2._1_2._2
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls=false;
}
publicvoidbtn1_change()
{
int i =0; thread1.Name="btn1_change";
for(;i <10;i++)
{
Threadthread1 =Thread.CurrentThread;
label1.Text=String.Format("{0},{1}",i, thread1.Name);
label1.Refresh();
Thread.Sleep(500);
}
}
publicvoidbtn2_change()
{
int i =0; thread2.Name="btn2_change";
for(;i <10;i++)
{
Threadthread2 =Thread.CurrentThread;
label2.Text=String.Format("{0},{1}",i,thread2.Name);
label2.Refresh();
Thread.Sleep(500);
}
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
Threadthread1 =newThread(newThreadStart(btn1_change));thread1.Start();
Threadthread2 =newThread(newThreadStart(btn2_change));
thread2.Start();
}
}
}
- 已标记为答案 Vortex.piggy 2012年2月12日 13:44
- 已编辑 Jacky_shen 2012年2月12日 13:48