重复邮件发送问题 [英] Duplicate Mail send Issue

查看:71
本文介绍了重复邮件发送问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们创建用于发送邮件的Windows服务。

我的邮件服务每2分钟启动一次。但是当服务启动时服务因首次运行服务而发送重复邮件未完成任务同时在2分钟内再次启动新窗口服务。

并从表中获取相同的数据并发送相同的邮件。

如何解决此问题?

解决方案

有很多方法可以实现这一目标。

1)在OnStart方法中,你可以使用while(true)循环调用你的代码,并可以在执行后放入睡眠。

 < span class =code-keyword> protected  覆盖  void  OnStart( string  [] args)
{
while true
{
SendMail();
Thread.Sleep( 2000 ); // 2秒睡眠
}
}





2)你可以使用Timer对象来做相同。



查看以下文章中的Timer对象。

http://www.codeguru.com/csharp/.net/cpp_managed/windowsservices/article.php/c6919 /Using-Timers-in-a-Windows-Service.htm [ ^ ]


以下方法尝试启动服务由服务名称指定。然后等待服务运行或发生超时。



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

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

// 在此处管理您的发送邮件代码
}
catch
{
// ...
}
}





希望有所帮助:)


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?

解决方案

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.

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[^]


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.

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 :)


这篇关于重复邮件发送问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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