Solved

How to make 2 threads run safely same block of code one after the other?

Posted on 2015-01-21
3
140 Views
Last Modified: 2015-01-21
Hi,

I have 3 threads running in my windows service application. If any exception in any of the threads, they have to send the email.
Below is my email code.
public static void SendMail(string machineName, string serviceName, string threadName, string errrorDesc)
        {
            Object syncLock = new Object();

            lock (syncLock)
            {
                try
                {
                    mm.Subject = _eMailTpltXML.Root.Element("Subject").Value.Replace("##MachineName##", machineName);
                    mm.Subject = mm.Subject.Replace("##ServiceName##", serviceName);
                    mm.Subject = mm.Subject.Replace("##ThreadName##", threadName);

                    mm.Body = _eMailTpltXML.Root.Element("Body").Value;
                    mm.Body = mm.Body.Replace("##MachineName##", machineName);
                    mm.Body = mm.Body.Replace("##ServiceName##", serviceName);
                    mm.Body = mm.Body.Replace("##ThreadName##", threadName);
                    mm.Body = mm.Body.Replace("##ErrorDesc##", errrorDesc);

                    sc.Send(mm);
                    Logger.log.Info(serviceName + " - Email has been sent.");
                }
                catch (Exception ex)
                {
                    Logger.log.Error("SendEmail - " + ex.Message);
                }
            }
        }

Open in new window


If the error occurs more than 2 threads at the same time, only one thread is able to send the email. Other 2 are failing and saying - "An asynchronous call is already in progress. It must be completed or canceled before you can call this method."

what is the best way to handle and make the 3 thread use the same block of code without any problems? (if the error occurs in 3 thread at the same time)
0
Comment
Question by:GouthamAnand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40563016
The lock object must be shared among your instances. One possible solution is to declare it as static (global variable)
public static Object syncLock = new Object();

Open in new window

and remove line 3 from your current posted code.

Note: Line 3 (syncLock = new Object())  is creating a new instance every time you call your method, thus it is not locking because the instances are different.
0
 

Author Comment

by:GouthamAnand
ID: 40563209
It worked for me. Thanks a lot.
0
 

Author Closing Comment

by:GouthamAnand
ID: 40563212
It worked for me.Thanks a lot.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question