2012-01-26 46 views
0

我有一个每10秒运行一次的windows服务来执行读取方法。 Read方法使用构造函数中提供的连接URL连接到远程服务器。 如果远程服务器无法响应,它会抛出错误并进行捕获。我们如何让线程重新开始?在Windows服务中使用线程异步执行C#方法

class PMTicketsService 
{ 
    private Timer _serviceTimer; 
    private TimerCallback _timerDelegate; 

    public PMTicketsService() 
    { 
     InitializeComponent(); 
    } 

    protected override void OnStart(string[] args) 
    { 
     // Set the method to execute when the timer executes. 
     _timerDelegate = new TimerCallback(Receive); 

     // Create timer and attach our method delegate to it 
     _serviceTimer = new Timer(_timerDelegate, null, 1000, 10000); 
    } 

    public void Receive(object state) 
    { 
     ABC abc = new ABC(Url); 
     ABC abc1 = new ABC(Url1); 

     /* Create the thread object, passing in the abc.Read() method 
     via a ThreadStart delegate. This does not start the thread. */ 
     Thread oThread = new Thread(new ThreadStart(abc.Read()); 
     Thread oThread1 = new Thread(new ThreadStart(abc1.Read()); 

     // Start the thread 
     oThread.Start(); 
     oThread1.Start(); 

     oThread.Join(); 
     oThread1.Join(); 
    } 
} 

class ABC 
{ 
    public string strUrl; 

    public ABC(string url) 
    { 
     strUrl = url; 
    } 

    public void Read() 
    { 
     try 
     { 
      // Code to use the connectionurl to access a remote server 
     } 
     catch (Exception ex) 
     { 
      // If the connection url fails to respond, how do we make thread start again? 
     } 
    } 
} 
+2

此代码可以使用一些清理... –

+0

@詹姆斯。你如何检查线程中的异常并启动另一个线程。你有代码示例吗? – srikanth

+0

什么版本的.NET? –

回答

1

将来,您应该提交实际编译的示例代码。我拿走了你已经清理过的东西,删除了不必要的计时器,并以一种应该给你所需要的方式构建它。在下面的代码中,您的Read方法将继续运行,直到您将done设置为true

protected override void OnStart(string[] args) 
    { 
     try 
     { 
      ABC abc = new ABC("www.abc.com"); 

      // Create the thread object, passing in the abc.Read() method 
      Thread oThread = new Thread(new ThreadStart(abc.Read)); 

      // Start the thread 
      oThread.Start(); 
     } 
     catch (Exception) 
     { 

     } 
    } 

    public class ABC 
    { 
     string strUrl = ""; 

     public ABC(string url) 
     { 
      strUrl = url; 
     } 

     public void Read() 
     { 
      bool done = false; 

      while (!done) 
      { 
       try 
       { 
        //Code to use the connectionurl to access a remote server 
        //Use strUrl in here 
       } 
       catch (Exception) 
       { 
        //Wait 10 seconds before trying again 
        Thread.Sleep(10000); 
       } 

       //On success, set done to true 
       done = true; 
      } 
     } 
    } 
1

为什么要开始另一个线程?启动/停止线程是一项昂贵的操作,你只需保持现有的线程处于打开状态并持续尝试连接(可能需要在两者之间休眠),情况会好得多。你已经有了try/catch来防止线程崩溃。只需在一段时间内完成try/catch(!完成),并在成功连接后将其设置为true。

您可能还需要添加一些代码,以便如果您无法连续连接X次(可能是5次),那么您将停止尝试,或增加连接尝试之间的超时时间。

+0

你有一个代码示例吗?我是一个线程的新手 – srikanth

+2

我刚刚告诉过你到底需要做什么,你需要做什么与线程无关;所有的线程都已经完成了。你需要定义一个布尔值,添加一个while循环,并且可能添加一个Thread.Sleep。这些是您需要添加的唯一代码行,并且我告诉过您需要添加它们的位置。我对你完全有信心,你可以把它变成源代码,而不需要在这里复制/粘贴它。 – Servy