C# 如何使用匿名方法设置线程名称?
我有以下代码来运行匿名线程方法的指令C# 如何使用匿名方法设置线程名称?,c#,.net,multithreading,C#,.net,Multithreading,我有以下代码来运行匿名线程方法的指令 new Thread(delegate() { //my code }).Start(); 出于调试的目的,我想在上面的场景中设置线程名称,我如何才能做到这一点 谢谢 或者让我们让它可读 var thread = Thread(delegate() { //my code }); thread.Name = "whatever"; thre
new Thread(delegate()
{
//my code
}).Start();
出于调试的目的,我想在上面的场景中设置线程名称,我如何才能做到这一点
谢谢
或者让我们让它可读
var thread = Thread(delegate()
{
//my code
});
thread.Name = "whatever";
thread.Start();
或者让我们让它可读
var thread = Thread(delegate()
{
//my code
});
thread.Name = "whatever";
thread.Start();
委托与线程名称无关:
Thread t = new Thread(delegate()
{
//my code
});
t.Name = "ThreadName";
t.Start();
委托与线程名称无关:
Thread t = new Thread(delegate()
{
//my code
});
t.Name = "ThreadName";
t.Start();
你有三个选择
- 不要将
放在同一行上.Start()
- 编写一个扩展方法
- (我甚至没想过要这么做,在写我的答案时看到了答案。)
- 不要将
放在同一行上.Start()
- 编写一个扩展方法
- (我甚至没想过要这么做,在写我的答案时看到了答案。)
- 您有三种选择
var thread = new Thread(delegate()
{
//my code
});
thread.Name = "MyName";
thread.Start();
static void Start(this Thread thread, string name)
{
thread.Name = name;
thread.Start();
}
//Elsewhere
new Thread(delegate()
{
//my code
}).Start("MyName");