Mou*_*dos 7 c# msmq thread-abort
我是一名Java程序员,他被要求对C#应用程序进行一些更改.我已经和C#一起工作了一个星期了,而且我终于找到了一个观察文档没有帮助的地方,当我谷歌时我找不到解决方案.
在这种情况下,我有一个Windows服务来处理到达MSMQ的消息.当收到一条消息时,当前正在侦听的线程将其拾取并开始执行需要几秒钟的操作.
public void Start()
{
this.listen = true;
for (int i = 0; i < Constants.ThreadMaxCount; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.StartListening), i);
}
...
private void StartListening(Object threadContext)
{
int threadId = (int)threadContext;
threads[threadId] = Thread.CurrentThread;
PostRequest postReq;
while(this.listen)
{
System.Threading.Monitor.Enter(locker);
try
{
postReq = GettingAMessage();
}
finally
{
System.Threading.Monitor.Exit(locker);
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
GettingAMessage()具有以下监听消息的行:
Task<Message> ts = Task.Factory.FromAsync<Message>
(queue.BeginReceive(), queue.EndReceive);
ts.Wait();
Run Code Online (Sandbox Code Playgroud)
问题是,当调用Stop()方法并且没有消息进入MSMQ时,线程都坐在那里等待消息.我尝试过使用超时,但这种方法对我来说似乎并不优雅(并且切换到了Task Factory,我不知道如何实现它们).我的解决方案是将每个线程的引用添加到数组中,以便我可以取消它们.创建后,每个工作线程调用以下内容.
threads[threadId] = Thread.CurrentThread;
Run Code Online (Sandbox Code Playgroud)
然后应该被中止
public void Stop()
{
try
{
this.listen = false;
foreach(Thread a in threads) {
a.Abort();
}
}
catch
{...}
}
Run Code Online (Sandbox Code Playgroud)
关于为什么不关闭线程的任何建议?(甚至更好,谁能告诉我应该在哪里找到如何正确取消ts.Wait()?)
使用ManualResetEvent该类可以正常和正常地停止正在运行的线程.
另外,不要使用ThreadPool长时间运行的线程,使用自己创建的线程,否则,对于许多长时间运行的任务,最终可能导致线程池饥饿,甚至可能导致死锁:
public class MsmqListener
{
privatec ManualResetEvent _stopRequested = new ManualResetEvent(false);
private List<Thread> _listenerThreads;
private object _locker = new _locker();
//-----------------------------------------------------------------------------------------------------
public MsmqListener
{
CreateListenerThreads();
}
//-----------------------------------------------------------------------------------------------------
public void Start()
{
StartListenerThreads();
}
//-----------------------------------------------------------------------------------------------------
public void Stop()
{
try
{
_stopRequested.Set();
foreach(Thread thread in _listenerThreads)
{
thread.Join(); // Wait for all threads to complete gracefully
}
}
catch( Exception ex)
{...}
}
//-----------------------------------------------------------------------------------------------------
private void StartListening()
{
while( !_stopRequested.WaitOne(0) ) // Blocks the current thread for 0 ms until the current WaitHandle receives a signal
{
lock( _locker )
{
postReq = GettingAMessage();
}
...
}
//-----------------------------------------------------------------------------------------------------
private void CreateListenerThreads()
{
_listenerThreads = new List<Thread>();
for (int i = 0; i < Constants.ThreadMaxCount; i++)
{
listenerThread = new Thread(StartListening);
listenerThreads.Add(listenerThread);
}
}
//-----------------------------------------------------------------------------------------------------
private void StartListenerThreads()
{
foreach(var thread in _listenerThreads)
{
thread.Start();
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
我改变了使用AutoResetEventwith ManualResetEvent以支持停止多个等待线程(使用ManualResetEvent,一旦发出信号,所有等待线程将被通知并可以自由地继续他们的工作 - 在你的情况下停止汇集消息).
使用volatile bool不提供所有保证.它仍然可以读取过时的数据.最好使用底层操作系统同步机制,因为它提供了更强大的保证.资料来源:stackoverflow.com/a/11953661/952310