【发布时间】:2019-11-07 12:28:26
【问题描述】:
我有两个功能。我需要在具有两侧或多个线程的单个线程上启动它们,但我希望它们被连接以便终止一个,也终止另一个。
public static void StartSocket()
{
Server myserver = new Server(*, ****);
}
public static void StartWeb()
{
CreateWebHostBuilder().Build().Run();
}
例子`
Thread t1 = new Thread(StartSocket);
Thread t2 = new Thread(StartWeb);
t1.Start();
t2.Start();
t2终止时如何终止t1,t1终止时如何终止t2。
【问题讨论】:
-
到目前为止您尝试过什么?此外,如果您将代码发布在您实际运行线程的位置,将会很有帮助。
-
@RobinB 完成,先生。
-
谢谢。好的,那么在您的上下文中,终止的确切含义是什么?如果
t1先完成,您要中止t2吗?或者您只是想在满足条件时同时取消两者? (这应该通过调用Abort()来解决。) -
看看这个:What's wrong with using Thread.Abort()。如需解决方案,请查看 cooperative cancellation 而不是中止线程。
标签: c# multithreading