【发布时间】:2012-11-05 23:10:52
【问题描述】:
我有一个快速连续调用的函数,它有一个打开的数据库连接。 我的问题是,在一个数据库连接关闭之前,该函数的另一个实例被调用,我可能会在数据库中收到死锁。
我试过了:
private static WaitHandle[] waitHandles = new WaitHandle[]
{
new AutoResetEvent(false)
};
protected override void Broadcast(Data data, string updatedBy)
{
Action newAction = new Action(() =>
{
DataManagerFactory.PerformWithDataManager(
dataManager =>
{
// Update status and broadcast the changes
data.UpdateModifiedColumns(dataManager, updatedBy);
BroadcastManager.Instance().PerformBroadcast(
data,
BroadcastAction.Update,
Feature.None);
},
e => m_log.Error(ServerLog.ConfigIdlingRequestHandler_UpdateFailed() + e.Message));
}
);
Thread workerThread = new Thread(new ThreadStart(newAction));
ThreadPool.QueueUserWorkItem(workerThread.Start, waitHandles[0]);
WaitHandle.WaitAll(waitHandles);
}
但我收到一个线程错误并且程序冻结。我相信它与没有参数的线程启动函数有关。
感谢您的帮助
【问题讨论】:
-
QueueUserWorkItem中的第二个对象是参数。见MSDN -
通常你不会在
ThreadPool上排队一个新线程的Start方法。这样就违背了目的。只需排队newAction。您可能应该锁定任何可能导致服务器端死锁的方法(PerformBroadcast?)。 -
ThreadPool.QueueUserWorkItem(newAction, null);不工作。 ThreadPool.QueueUserWorkItem(newAction, waitHandles[0]);不工作。
标签: c# multithreading concurrency waithandle