简体   繁体   English

我如何在c#中等待一段时间(Windows服务)

[英]How I can waiting some time in c# (windows service)

I created windows service on C#. 我在C#上创建了windows服务。 For now I have methods for scanning DB. 现在我有扫描DB的方法。 I need call this method two times per minute. 我需要每分钟调用这个方法两次。 Actually I don't know method for waiting in windows service. 其实我不知道在Windows服务中等待的方法。 I tried Thread.Sleep... but nothing happened. 我试过Thread.Sleep ......但什么都没发生。 Please help me with this problem. 请帮我解决这个问题。

private int wait;
protected void Start()
{
    wait = 1000;
    while (true)
    {
        if (wait < 30000)
            wait += wait;

        //implement logic for waiting

        Video video = new Video();
        video.FindFileForConvert();
        if (video.Path != null)
        {
            Console.WriteLine("video != null. video path = {0}", video.Path);
            video.BeginConvertation();
            video.DeleteOriginFile();
            wait = 1000;
        }
    }
}

You should use System.Threading.Timer for the same. 您应该使用System.Threading.Timer Since Thread.sleep is not a good practice atleast in some cases. 由于Thread.sleep在某些情况下至少不是一个好习惯。

You may use Timer 你可以使用Timer

public static int Main() {
   /* Adds the event and the event handler for the method that will 
      process the timer event to the timer. */
   myTimer.Tick += new EventHandler(TimerEventProcessor);

   // Sets the timer interval to 5 seconds.
   myTimer.Interval = 5000;
   myTimer.Start();

   // Runs the timer, and raises the event. 
   while(exitFlag == false) {
      // Processes all the events in the queue.
      Application.DoEvents();
   }
return 0;
}

I may be mistaken, but I think that this code will help you resolve your problem. 我可能弄错了,但我认为这段代码可以帮助您解决问题。 DispatcherTimer DispatcherTimer

DispatcherTimer dispathcerTimer = new DispatcherTimer();
dispathcerTimer.Interval = TimeSpan.FromMinutes(2);
dispathcerTimer.Tick += dispathcerTimer_Tick;
dispathcerTimer.Start();

void dispatcherTime_Tick(object sender, object e)
{
  //function, which needs to be invoked every two minutes.     
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C# 中发送带有等待时间的电子邮件? - How can I send emails with waiting time in C#? 如何在C#中的Windows服务中执行批处理脚本? - How can I execute a batch script in a windows service in C#? 如何在用C#编写的Windows服务中获取ServiceName? - How can I get the ServiceName in a Windows Service written in C#? 是否为C#Windows服务提供OnRestart()事件侦听器? 如何查看Windows服务是否已重新启动? - Is there a OnRestart() event listener for a C# Windows service? How can I see if the Windows Service was Restarted? 如何在C#中创建可以与GUI *或*一起运行的Windows应用程序? - How can I create a Windows application that can run with a GUI *or* as a Windows service in C#? 我如何使用C#写入Windows事件日志(我遇到某种错误)? - How can i write to the windows event log using C# (I'm getting some kind of error)? C#如何在Windows共享中找到与资源的连接并删除其中的一些 - C# How can i find connections to resources in a windows share and drop some of them 我如何在 Windows 通用应用程序 C# 中发布一个 json 字符串以及一些参数 - how can i post a json string along with some parameters in windows universal application c# C# - 具有系统时间意识的Windows服务 - C# - Windows Service with awareness of System Time C# - 如何在一段时间内阻止方法返回? - C# - How can I block a method return for some interval of time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM