什么是错在这的Parallel.For code? [英] Whats wrong in this Parallel.For Code?

查看:311
本文介绍了什么是错在这的Parallel.For code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要运行的code。

this is the code that i want to run.

Parallel.For(1, itemCount, 1, () =>
                    {
                     return new ThreadLocalStateCache()
                     {
                         //assigning values to local variables
                         Receipient = serMailObj.ReceipientList.Dequeue(), //get a single recepeint for the email
                         mail = serMailObj.Email, //Object of type MailMessage
                         client = client //object of type SmtpClient
                     };
                }
     , (i, loopState) =>
     {
         doWork(i, loopState.ThreadLocalState);

     });
                }
//class to store local vairables for each thread
public class ThreadLocalStateCache
    {
        public KeyValuePair<string, string> Receipient { get; set; }

        public MailMessage mail { get; set; }

        public SmtpClient client { get; set; }
    }

private static void doWork(int instance, ThreadLocalStateCache threadInstance)
        { 
           //send mail
        }

和它口口声声说

有关方法的类型参数System.Threading.Tasks.Parallel.For(很长很长,System.Func,System.Func,System.Action)'不能从使用推断。请尝试显式指定类型参数。

The type arguments for method 'System.Threading.Tasks.Parallel.For(long, long, System.Func, System.Func, System.Action)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

我找不到解释清楚如何使用的Parallel.For使用线程局部变量在互联网上的任何资源。我试图处理电子邮件收件人的长长的名单,并发送邮件给他们。请告诉我如何使用的Parallel.For。

I could not find any resource on the internet that explains clearly how to use parallel.for with thread local variables. I am trying to process long list of email recipients and send mails to them. Please tell how can i use parallel.for.

修改1:我读这篇文章后尝试此code的 http://www.lovethedot.net/2009/02/parallelfor-deeper-dive-parallel.html

EDIT 1: I am trying this code after reading this article http://www.lovethedot.net/2009/02/parallelfor-deeper-dive-parallel.html

推荐答案

本的Parallel.For重载走一步,第三个参数是从.NET 4中删除;看评论 http://blogs.msdn.com/ B / pfxteam /存档/ 2009年/ 5月26日/ 9641563.aspx

The Parallel.For overloads that take step as the third argument were removed from .NET 4; see comments to http://blogs.msdn.com/b/pfxteam/archive/2009/05/26/9641563.aspx.

由于是,有5个参数呼叫决心此重载

Due to that, your call with 5 arguments is resolved to this overload:

For<TLocal>(Int32, Int32, Func<TLocal>, Func<Int32, ParallelLoopState, TLocal, TLocal>, Action<TLocal>)

和明显的编译器无法比拟类型的参数。

And obviously the compiler cannot match types of the arguments.

由于步长为1,无论如何,只是将其删除。
然后,你将需要修复身体委托其中必须有三个参数(因为线程局部变量现在从循环状态对象分开),并添加将应用于线程局部变量的最终计算的另一个代表。最后,它应该是这样的:

Since the step is 1 anyway, just remove it.
Then you will need to fix the body delegate which must have three parameters (since thread local variable is now separate from loop state object), and add another delegate that will be applied to thread local variables for final computation. At the end, it should be something like this:

Parallel.For( 1, itemCount,
              () =>
              { return new ThreadLocalStateCache() 
                           { 
                               Receipient = serMailObj.ReceipientList.Dequeue(),
                               mail = serMailObj.Email,
                               client = client
                           }; 
              },
              (i, loopState, threadLocal ) => 
              { 
                  doWork(i, threadLocal);
                  return threadLocal;
              },
              (threadLocal) => {}
            );

这篇关于什么是错在这的Parallel.For code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆