创建人工线程 - 但越来越重复的主题 [英] Creating manual threads - but getting duplicate threads

查看:109
本文介绍了创建人工线程 - 但越来越重复的主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:获得重复的项目,即越来越比环路阵列中的每个元素的数组的大小...
嗨伙计们,我创建线程创建多个线程。真正的用途是发送批量使用亚马逊SES消息的那个。 。该消息被存储在messageamazonRequestBatch和循环通过批处理运行,并将消息发送

ISSUE: Getting duplicate items, i.e more threads are getting created than the array size... Hi Folks, I am creating thread in the loop for each element of array. The real use is that the of sending a batch of messages using amazon ses. the messages are stored in the messageamazonRequestBatch and the loop runs through the batch and sends the messages.

这里是代码:

Thread thrdSendEmail;
            try
            {
                string amazonMessageID = string.Empty;
                List<Thread> lstThread = new List<Thread>();
                foreach (int n in arrMessageid)
                {
                    thrdSendEmail = new Thread(() =>
                    {
                            try
                            {
                                amazonMessageID = SendSimpleEmail_Part2(messageAmazonRequestBatch.ElementAt(n).req);
                                messageAmazonRequestBatch.ElementAt(n).msg.AmazonMessageID = amazonMessageID;
                                logManager_MessageLogwithAmazonmsgID.LogMessage(",\t" + n , true);
                                //logManager_MessageLogwithAmazonmsgID.LogMessage(",\t" + n + ",\t" + messageAmazonRequestBatch.ElementAt(n).msg.QueueMessageId + ",\t" + amazonMessageID, true);
                            }
                            catch (Exception ex) { logManager_RunSummary.LogMessage(ex.Message, true); }                                
                    });
                    thrdSendEmail.Name = n.ToString();
                    lstThread.Add(thrdSendEmail);
                    thrdSendEmail.Start();
                    //logManager_MessageLogwithAmazonmsgID.LogMessage(",\t" + n, true);
                }
                foreach (Thread t in lstThread)
                {
                    t.Join();
                    //logManager_MessageLogwithAmazonmsgID.LogMessage(",\t" + t.Name, true);
                }
            }
            catch (Exception ex)
            {
                logManager_RunSummary.LogMessage(ex.Message, true);
            }



我自己也尝试parallel.foreach和非同步并等待选项...他们也给副本。我知道锁将解决这个问题,但在我的情况下,锁被降低了10倍的性能..这是我的表现降到10倍......堂妹把sendemail登录锁定阻塞,直到我获得回报amazonmessageid从亚马逊...

I have also tried parallel.foreach and asynch and await options... they also give the duplicates. I know that the lock will solve the problem but in my case the lock degrades the performance by a factor of 10.. that is my performance drops 10 times... coz putting the sendemail login in lock is blocking untill i get a return amazonmessageid from amazon...

任何帮助将不胜感激。我不是一个新手程序员,但新线程...

Any help on this will be greatly appreciated. I am not a novice programmer but new to threading...

推荐答案

您lambda表达式是捕获循环变量<$ C $ ç> N ,所以当你的lambda执行的 N 已经改变的价值;你需要复制 N 来循环中的局部变量。 (假设你使用C#4或更早版本; C#5固定的问题)。

Your lambda expression is capturing the loop variable n, so when your lambda executes, the value of n has already changed; you need to copy n to a local variable inside the loop. (assuming you're using C# 4 or earlier; C# 5 fixed that problem).

另一个问题是,所有的线程使用相同的 amazonMessageID 变量;你应该把它声明的lambda表达式,而不是内部。

Another issue is that all your threads use the same amazonMessageID variable; you should declare it inside the lambda expression instead.

            foreach (int n in arrMessageid)
            {
                int n2 = n;
                thrdSendEmail = new Thread(() =>
                {
                        try
                        {
                            string amazonMessageID = SendSimpleEmail_Part2(messageAmazonRequestBatch.ElementAt(n2).req);
                            messageAmazonRequestBatch.ElementAt(n2).msg.AmazonMessageID = amazonMessageID;
                            logManager_MessageLogwithAmazonmsgID.LogMessage(",\t" + n2 , true);
                            //logManager_MessageLogwithAmazonmsgID.LogMessage(",\t" + n2 + ",\t" + messageAmazonRequestBatch.ElementAt(n2).msg.QueueMessageId + ",\t" + amazonMessageID, true);
                        }
                        catch (Exception ex) { logManager_RunSummary.LogMessage(ex.Message, true); }                                
                });
 ...

这篇关于创建人工线程 - 但越来越重复的主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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