2015-02-10 73 views
1

根據這個Stack Overflow discussion,使用Thread.Sleep()幾乎總是一個壞主意。我將如何重構我的代碼來使用計時器。我試着做以下,使一個開始:用定時器替換代碼而不是線程休眠

namespace Engine 
{ 
    internal class Program 
    { 
     public static DbConnect DbObject = new DbConnect(); 

     System.Timers.Timer timer = new System.Timers.Timer(); 

     // error here 
     timer.Interval = 2000; 
     timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 
     timer.Enabled=false; 
    } 
} 

但持續接受cannot resolve symbol錯誤消息。

namespace Engine 
{ 
    internal class Program 
    { 
    public static DbConnect DbObject = new DbConnect(); 
    private static void Main() 
    { 
     SettingsComponent.LoadSettings(); 

     while (true) 
     { 
      try 
      { 
       for (int x = 0; x < 4; x++) 
       { 
        GenerateRandomBooking(); 
       } 
       Thread.Sleep(2000); 
       GenerateRandomBids(); 
       AllocateBids(); 
       Thread.Sleep(2000); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.ToString()); 
      } 
     } 
    } 
    } 
} 
+1

你必須把你的計時器initalization成方法,例如在主要的開始。 – BoeseB 2015-02-10 10:28:12

回答

2

如果您使用的是.NET 4.5或更高版本,可以使用await而不是一個定時器。

例如:

using System; 
using System.Threading; 
using System.Threading.Tasks; 

namespace Demo 
{ 
    public static class Program 
    { 
     private static void Main() 
     { 
      Console.WriteLine("Generating bids for 30 seconds..."); 

      using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30))) 
      { 
       var task = GenerateBids(cancellationTokenSource.Token); 

       // You can do other work here as required. 

       task.Wait(); 
      } 

      Console.WriteLine("\nTask finished."); 
     } 

     private static async Task GenerateBids(CancellationToken cancel) 
     { 
      while (!cancel.IsCancellationRequested) 
      { 
       Console.WriteLine(""); 

       try 
       { 
        for (int x = 0; x < 4; x++) 
         GenerateRandomBooking(); 

        await Task.Delay(2000); 

        if (cancel.IsCancellationRequested) 
         return; 

        GenerateRandomBids(); 
        AllocateBids(); 

        await Task.Delay(2000); 
       } 

       catch (Exception e) 
       { 
        Console.WriteLine(e); 
       } 
      } 
     } 

     private static void AllocateBids() 
     { 
      Console.WriteLine("AllocateBids()"); 
     } 

     private static void GenerateRandomBids() 
     { 
      Console.WriteLine("GenerateRandomBids()"); 
     } 

     private static void GenerateRandomBooking() 
     { 
      Console.WriteLine("GenerateRandomBooking()"); 
     } 
    } 
} 
1

您可以將您的代碼這一個不使用Thread.sleep()方法

private static void Main() 
    { 
     SettingsComponent.LoadSettings(); 

     //while (true) 
     { 
      try 
      { 
       RaiseRandomBooking(null); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.ToString()); 
      } 
     } 
    } 

    static void RaiseRandomBooking(object state) 
    { 
     for (int x = 0; x < 4; x++) 
     { 
      GenerateRandomBooking(); 
     } 
     System.Threading.Timer tmr = new System.Threading.Timer(RaiseRandomBids, null, 0, 2000); 
    } 

    static void RaiseRandomBids(object state) 
    { 
     GenerateRandomBids(); 
     AllocateBids(); 
     System.Threading.Timer tmr = new System.Threading.Timer(RaiseRandomBooking, null, 0, 2000); 
    } 
+0

在RaiseRandomBooking for循環之後,該代碼不會退出嗎?因爲在此之後沒有任何東西阻塞主線程中的執行 – BoeseB 2015-02-10 10:45:13

+0

@BoeseB,RaiseRandomBooking()方法的執行將退出,但計時器委託在後臺運行。 – 2015-02-10 10:48:57

+0

因此,MainThread會運行結束,控制檯會在委託執行之前關閉?所以整個應用程序被殺害,或者我得到這個錯誤? – BoeseB 2015-02-10 10:50:23