这段 Parallel.For 代码有什么问题吗?

发布于 2024-10-31 07:51:42 字数 1521 浏览 0 评论 0原文

这是我想要运行的代码。

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(long, long, System.Func, System.Func, System.Action)' 的类型参数。尝试显式指定类型参数。

我在互联网上找不到任何资源可以清楚地解释如何将parallel.for 与线程局部变量一起使用。我正在尝试处理一长串电子邮件收件人并向他们发送邮件。请告诉我如何使用parallel.for。

编辑1:阅读本文后我正在尝试此代码http://www.lovethedot.net/2009/02/parallelfor-deeper-dive-parallel.html

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
        }

and it keeps on saying

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光清浅 2024-11-07 07:51:42

将 step 作为第三个参数的 Parallel.For 重载已从 .NET 4 中删除;请参阅 http://blogs.msdn.com/ 的评论b/pfxteam/archive/2009/05/26/9641563.aspx

因此,带有 5 个参数的调用将解析为此重载

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

显然编译器无法匹配参数的类型。

由于步骤无论如何都是 1,只需将其删除即可。
然后,您需要修复必须具有三个参数的主体委托(因为线程局部变量现在与循环状态对象分开),并添加另一个委托,该委托将应用于线程局部变量以进行最终计算。最后,它应该是这样的:

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) => {}
            );

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.

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.

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) => {}
            );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文