C#锁(mylocker)无法正常工作 [英] C# lock(mylocker) not work
问题描述
我有很多的Web服务调用(异步),在回调,我将绘制导致到Excel。
我要同步的情节方法。
所以我用下面的,不过,从我在Visual Studio中跟踪,每一次,锁(柜)是成功的,并且有运行clearcommentIfany,情节多个线程。
我想不通,为什么预期这不工作!谢谢
私人只读对象更衣室=新的对象();
无效ProcessPlot()
{
锁(柜)
{
的Debug.WriteLine(currentThreadID:+ Thread.CurrentThread.ManagedThreadId);
//Helper.Dispatcher.Invoke(new AddClearCommentDelegate(ClearCommentIfAny));
ClearCommentIfAny();
如果(Response.status = Status.COMPLETE!)
{
的ErrorMessage = ManipulateStatusMsg(响应);
//Helper.Dispatcher.Invoke(new AddClearCommentDelegate(AddCommentToCell));
AddCommentToCell();
}
,否则// COMPLETE
{
// TODO:转换到这一工厂模式
的Debug.WriteLine(ReportBuilder.Dispatcher地址+ Helper.GetAddress( Helper.Dispatcher));
//Helper.Dispatcher.Invoke(new PlotDelegate(情节),响应);
地块(响应);
}
}
}
公共无效DataRequestJobFinished(DataRequestResponse响应)
{
试
{
如果(Request.IsRequestCancelled)
{
Request.FormulaCell.Dispose();
的回报;
}
响应=响应;
//如果(response.status = Status.COMPLETE!)
// {
//的ErrorMessage = ManipulateStatusMsg(响应);
//}
// //其他COMPLETE
// {
// // TODO:转换到工厂模式
// PlotDelegate plotDelegate =该地块;
//的Debug.WriteLine(ReportBuilder.Dispatcher地址+ Helper.GetAddress(Helper.Dispatcher));
// Helper.Dispatcher.Invoke(plotDelegate,响应);
// //图(响应);
//}
变种T =新的Thread(ProcessPlot);
t.Start();
// ProcessPlot();
}
赶上(例外五)
{
的ErrorMessage = e.Message;
MIMICShared.Helper.LogError(E);
}
终于
{
// TODO:PutReportBuilderInQueue(本);
ReadyForPlot = TRUE;
//Request.FormulaCell.Dispose();情节
UnityContainer.Resolve℃后迁此; IEventAggregator>()GetEvent< DataRefreshEvent方式>()发布(ID);
}
}
我怀疑这里的问题是,你的锁是一个实例成员,而不是静态的(类型级别)的成员。
假设每个线程都有自己的包含类的一个实例,然后它还会有自己的更衣柜实例 - 这是不是你想要的。
试试这个声明来代替:
私有静态只读对象更衣室=新的对象();
static关键字的加入使得现在这个对象实例在类型级别的存在,即在所有共享您的类的实例。
I have many web service call (asychronous), in callback, I will plot result to Excel. I want to synchronize the plot method. So I use the following, however, from I track down in Visual Studio, every time, lock(locker) is successful, and there are many threads running clearcommentIfany, plot. I can't figure out why this is not working as expected! Thanks
private readonly object locker = new object();
void ProcessPlot()
{
lock (locker)
{
Debug.WriteLine("currentThreadID: " + Thread.CurrentThread.ManagedThreadId);
//Helper.Dispatcher.Invoke(new AddClearCommentDelegate(ClearCommentIfAny));
ClearCommentIfAny();
if (Response.status != Status.COMPLETE)
{
ErrorMessage = ManipulateStatusMsg(Response);
//Helper.Dispatcher.Invoke(new AddClearCommentDelegate(AddCommentToCell));
AddCommentToCell();
}
else // COMPLETE
{
// TODO: Convert this into factory pattern
Debug.WriteLine("ReportBuilder.Dispatcher's address " + Helper.GetAddress(Helper.Dispatcher));
//Helper.Dispatcher.Invoke(new PlotDelegate(Plot), Response);
Plot(Response);
}
}
}
public void DataRequestJobFinished(DataRequestResponse response)
{
try
{
if (Request.IsRequestCancelled)
{
Request.FormulaCell.Dispose();
return;
}
Response = response;
//if (response.status != Status.COMPLETE)
//{
// ErrorMessage = ManipulateStatusMsg(response);
//}
//else // COMPLETE
//{
// // TODO: Convert this into factory pattern
// PlotDelegate plotDelegate = Plot;
// Debug.WriteLine("ReportBuilder.Dispatcher's address " + Helper.GetAddress(Helper.Dispatcher));
// Helper.Dispatcher.Invoke(plotDelegate, response);
// //Plot(response);
//}
var t = new Thread(ProcessPlot);
t.Start();
//ProcessPlot();
}
catch (Exception e)
{
ErrorMessage = e.Message;
MIMICShared.Helper.LogError(e);
}
finally
{
//TODO: PutReportBuilderInQueue(this);
ReadyForPlot = true;
//Request.FormulaCell.Dispose(); move this after plot
UnityContainer.Resolve<IEventAggregator>().GetEvent<DataRefreshEvent>().Publish(ID);
}
}
I suspect your problem here is that your lock is an instance member rather than a static (type level) member.
Assuming that each thread has its own instance of the containing class, then it'll also have its own instance of your locker - which is not what you want.
Try this declaration instead:
private static readonly object locker = new object();
The inclusion of the static keyword now makes this object instance exist at the type level, i.e. shared across all instances of your class.
这篇关于C#锁(mylocker)无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!