Socket Server and NUnit: Thread.Sleep

风格不统一 提交于 2020-05-30 08:00:08

问题


I am writing a socket server and client classes, which I'm testing through NUnit.

I wrote a process, which is checking all clients, if they are still connected. For this process I created a Thread, but what I want is to run this check only like every 5 seconds. My first intension was to use Thread.Sleep(). When I am ending the test, I am running the Stop method on server, which interrupt all active threads, which is causing that Thread was interruted from a waiting state.

I find out there is alternative through the Task.Delay().Wait(), but this is causing the same problem.

Is there any other alternative to Thread.Sleep, or any other way, when I am closing server to end this thread? The thread is running while loop with condition Thread.CurrentThread.IsAlive.


回答1:


As answered in my other question, I found a better solution, because I want run the check only in periods of time (like 5 seconds or so), the good usage here is System.Threading.Timer class, which is suitable for the periodacal run of methods.



来源:https://stackoverflow.com/questions/61727572/socket-server-and-nunit-thread-sleep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!