我已經繼承了一個多線程Windows服務(C#,.NET),但不支持無端異常。該服務使用多個線程從遙測設備收集數據進行分析。此服務中的線程可以運行24小時或更長時間。我正在努力如何告訴主線程線程遇到問題並需要回收。下面是代碼的簡化視圖:線程:使用C#調用返回到.NET Windows服務的主線程
class Program
{
static void Main(string[] args)
{
var workerObject = new WorkerObject();
var workerThread = new Thread(workerObject.DoWork);
workerThread.Start()
}
}
class WorkerObject
{
//todo: My fields and properties go here
public void DoWork()
{
const int timeout = 100;
//todo: setup wait handles here
try
{
//Start monitoring channels for this device
while (true)
{
// Wait on any waithandle or timeout once per decisecond.
int channelIndex = WaitHandle.WaitAny(waitHandles, timeout, false);
//todo: process incoming data for this channel
}
}
//Catch all exceptions so we can notify mommy that we're in a bad way
catch (Exception ex)
{
//todo: log anything that can help us figure this problem out
//How do I tell the main thread I've failed
}
finally
{
//But of course we want to clean up after ourselves
}
}
}
這是否甚至是最好的.NET線程機制?我應該使用Async Delegates還是Task Parallel Library?
哥們你可以在你的代碼中添加一個鏈接到源代碼?不可能理解任何事情。 – IamStalker 2015-01-29 09:35:33