新手,问个有关多线程的问题.

ioriyzsnfdf 2007-12-15 03:04:44
问一下,如果我启动了两个线程,调用的同一个过程(不是同时),我如何在过程里判断现在是哪个线程在运行它?谢谢.
...全文
208 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuadam 2007-12-16
  • 打赏
  • 举报
回复
可以给回调方法传一个参数
绝代坏坏 2007-12-16
  • 打赏
  • 举报
回复
一楼正解
jinjazz 2007-12-16
  • 打赏
  • 举报
回复
private void Form1_Load(object sender, EventArgs e)
{
System.Threading.Thread thread1 = new System.Threading.Thread(new System.Threading.ThreadStart(test));
thread1.Name = "thread1";
System.Threading.Thread thread2 = new System.Threading.Thread(new System.Threading.ThreadStart(test));
thread2.Name = "thread2";

thread1.Start();
thread2.Start();
}

private void test()
{
Console.WriteLine(System.Threading.Thread.CurrentThread.Name);
}
Damon_King 2007-12-16
  • 打赏
  • 举报
回复
楼上仁兄的说的是:System.Threading.Thread.CurrentThread.ManagedThreadId 。
我认为,若你想自己判断是哪个线程在执行,可以在定义线程的时候给线程的Name属性赋值。因为使用楼上这位仁兄说的ManagedThreadId这个属性,你是无从判断哪个线程在执行当前过程的。
ouhou 2007-12-15
  • 打赏
  • 举报
回复
在线程调用的过程中判断,System.Threading.Thread.CurrentThread.ManagedId 好像是这么写的,能得到线程的ID。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧