English 中文(简体)
Rx 任务。 工厂。 开始新开始两项任务,涉及Net 3.5问题
原标题:Rx Task.Factory.StartNew starts two tasks on .Net 3.5 issue

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);
}
最佳回答
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签