Im利用Reactive Expansions Library on .Net 3.5的任务部分。
它的工作大多顺利进行,但是在一个地方,它两次提出同样的任务。
这一呼吁是:
Task.Factory.StartNew(
() => Processor.ProcessMessage(incomingMessage),
TaskCreationOptions.PreferFairness );
任何想法? 这难道是一种ug吗?
页: 1
i think the problem is with the way c# does closure in lambdas. the problem wasn"t in the TPL, the same problem returned with the plain old thread pool.
并解决了这一问题:
foreach (var Processor in processors)
{
object[] crap = new object[2];
crap[0] = Processor;
crap[1] = incomingMessage;
Task.Factory.StartNew(Magic, crap, TaskCreationOptions.PreferFairness);
}
public void Magic(object obj)
{
object[] crap =(object[]) obj;
((IIncomingMessageProcessor)crap[0]).ProcessMessage((IncomingMessageBase)crap[1]);
}
原始来源是:
foreach (var Processor in processors)
{
Task.Factory.StartNew(
() => Processor.ProcessMessage(incomingMessage),
TaskCreationOptions.PreferFairness );
}
这样一来,在加工商周围就关闭了,而我猜测问题在于,它正在把同样的物体回收到lam中,并对加工商进行冲撞。
页: 1 2
I m convinced this is the problem. i refactored and debugged the System.Threading.dll both times i create the task, it is created with the same delegate(Same ObjectID) and the Processor changes in the Target property between iterations. anyone knows a good work around?
页: 1 3 this works too(thanks Judah Himango):
foreach (var processor in processors)
{
var crap = processor;
Task.Factory.StartNew(() => crap.ProcessMessage(incomingMessage), TaskCreationOptions.PreferFairness);
}