I have a piece of code where I send a file content over tcp/ip channel. There are times when this connection hangs causing entire application to freeze. Is there a way for my main thread to spawn a worker thread and monitor that worker thread. If worker thread succeeds, well and good. If it hangs , the main thread could log error message and continue. How can I simulate in my test code that a worker thread is hanging. 开发者_高级运维please let me know what could the code look like. I am using C# Visual studio 2002.
Surely this is possible.
Either you implement threading manually using the BackgroudWorker or Thread class or (in your case even simpler) you use the asynchroneous methods for sending your content.
All the network related classes contain asynchroneous methods for their operations. Look for the methods that contain Async
or Begin
...
And simulating a dead thread is simple. Just make an endless loop:
while (true)
System.Threading.Thread.Sleep (10);
精彩评论