C#-线程处理任务-Dotnet 4.x中的最佳实践

时间:2018-12-23 16:50:23

标签: c# multithreading

在.NET 2.x中,我正在使用

 var SocketStart = new Thread(() => new WebSocketManager().Socket()); //to prepare websocket manager in background.
 SocketStart.Start(); //to start ws manager.
 var SocketStart2 = new Thread(() => new WebSocketManager().Socket2()); //to prepare websocket manager in background.
 SocketStart2.Start(); //to start ws manager.
 if(SomeDepedencyIsDead) //chacking if some stuff is working? 
   {
     SocketStart().Suspend(); //deprecaced
     SocketStart2().Suspend(); //deprecaced
      while(SomeDepedencyIsDead)
        {
           Wait();
        }
    SocketStart.Resume(); //deprecaced
    SocketStart2.Resume(); //deprecaced
    }

现在

  

SocketStart()。Suspend()已过时&&   不推荐使用SocketStart()。Resume()

这给了我错误

  

System.PlatformNotSupportedException:“此平台不支持线程挂起。”

那么,暂停和启动线程的最佳实践是什么?我应该使用 new Thread(())等吗?还有什么比 System.Threading 库更好的线程管理方法吗?

1 个答案:

答案 0 :(得分:1)

尝试以下

var Socket1 =  new WebSocketManager(); //to prepare websocket manager in background.
 ... //to start ws manager.
 var Socket2 = new WebSocketManager(); //to prepare websocket manager in background.
 ... //to start ws manager.


      while(SomeDepedencyIsDead)
        {
           Thread.Sleep(2000);
        }