Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
We create Windows Service For Sending Mail.
MY mail Service Started Every 2 minute .But When Service started Service Send Duplicate Mail Because of First Running Service Not Completed Task in 2 Minute at same time again new window service started.
and take same data from table and send same mail.
how to resolve this issue?
Posted

There are many ways to achieve this.
1) in OnStart method you can use call your code in a while(true) loop and can put sleep after execution.
C#
protected override void OnStart(string[] args)
{
    while(true)
    {
          SendMail();
          Thread.Sleep(2000);//2 second sleep
    }
}


2) You can use Timer object to do the same.

Check below article for Timer object.
http://www.codeguru.com/csharp/.net/cpp_managed/windowsservices/article.php/c6919/Using-Timers-in-a-Windows-Service.htm[^]
 
Share this answer
 
Comments
Rahulgupta.hanu 6-Jan-15 9:06am    
I used Thread.sleep But Issue Not Resolved.
Praveen Kumar Upadhyay 6-Jan-15 9:30am    
Can you make a file log after sending the mail and check if file log is two times. Sometimes it happens with the mail server also and it sends the mail two times.
Otherwise the code which I have mentioned will definitely work.
The following method tries to start a service specified by a service name. Then it waits until the service is running or a timeout occurs.

C#
public static void StartService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);

    //Manage your send mail code here
  }
  catch
  {
    // ...
  }
}


Hope it helps :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900