2012-05-16 55 views
2

即時通訊新手段。我想調用一個每隔幾秒鐘就會返回一個值的方法。 我已經嘗試使用與elapsedEventandler計時器。但是在這種情況下該方法的返回類型是無效的。 我已經使用TimerTask類在java中執行相同的任務。調用一個方法,每5秒返回一個值C#

對此的任何幫助將不勝感激。我希望它在.net 2.0中,因爲我使用visual studio 2005.

下面是我遇到麻煩的程序,我試圖使用匿名方法,但「響應」變量的值這種情況下,不匿名方法之外存在:

 public static string Run(string address) 
    {      
     string response = "A"; 
     Timer t = new Timer(); 
     t.Elapsed += delegate {       
      response = callURL(address); 
      console.writeln(response); 
      //The actual response value is printed here     
     };   
     t.Interval = 3000; 
     t.Start();      
     Console.WriteLine("response string is " +response); 
     //response string is A 

     return response; 
    } 


    public static string callURL(string address) 
    { 
     className sig = new ClassName(); 
     String responseBody = sig.getURL(address); 

     return responseBody; 
    } 

的一點是:我想在run方法響應的值,並將其發送到RUN方法的調用者。

+2

返回到*哪裏*?計時器可以啓動你的方法,但沒有人會直接調用它來接收它的結果。 –

+0

它每5秒調用一次該方法,還是僅在返回後每5秒調用一次? I.E,如果調用時間太長,可以調用堆棧嗎? – Tejs

+0

@james:它從另一個類返回給調用者。 – Bharath

回答

6

你可以有你的來電者給類的計時器回調委託傳遞迴到價值。

public class YourClass 
{ 
    public static void Run(string address, Action<string> callback) 
    {      
     Timer t = new Timer(); 
     t.Elapsed += delegate {       
      var response = callURL(address); 

      callback(response); 
     };   
     t.Interval = 3000; 
     t.Start();      
} 


public class OtherClass 
{ 
    public void ProcessResponse(string response) 
    { 
     // do whatever you want here to handle the response... 
     // you can write it out, store in a queue, put in a member, etc. 
    } 


    public void StartItUp() 
    { 
     YourClass.Run("http://wwww.somewhere.net", ProcessResponse); 
    } 
} 

UPDATE:如果你想調用者(OtherClass)能夠取消計時器,你可以簡單地從Action<string>更改爲Func<string, bool>並有來電(OtherClass)是否返回一個布爾停止定時器或不...

public class YourClass 
{ 
    public static void Run(string address, Func<string, bool> callback) 
    {      
     Timer t = new Timer(); 
     t.Elapsed += delegate {       
      var response = callURL(address); 

      // if callback returns false, cancel timer 
      if(!callback(response)) 
      { 
       t.Stop(); 
      } 
     };   
     t.Interval = 3000; 
     t.Start();      
} 


public class OtherClass 
{ 
    public bool ProcessResponse(string response) 
    { 
     // do whatever you want here to handle the response... 
     // you can write it out, store in a queue, put in a member, etc. 

     // check result to see if it's a certain value... 
     // if it should keep going, return true, otherwise return false 
    } 


    public void StartItUp() 
    { 
     YourClass.Run("http://wwww.somewhere.net", ProcessResponse); 
    } 
} 
+0

哇,這是快速和真棒。謝謝。 :) – Bharath

+0

隨時!當然,你仍然需要編寫代碼來處理你的回覆,但是至少可以給你框架... –

+0

我從昨天就迷失了。其餘的不應該是我想的問題。這是主要的事情。我試圖將代碼從Java轉換爲C#。我在這裏迷路了。 – Bharath

0

創建一個線程,使線程中調用需要的方法並給予睡眠時間到線程5秒

Thread loopThread= new Thread(new ThreadStart(this.MainLoop)); 
    loopThread.Start(); 

    //Main Loop 
    private void MainLoop() 
    { 
     while(true) 
     { 
      //do stuff 
      Thread.Sleep(5000); 
     } 
    } 
+0

使用定時器不可能做到這一點嗎?我想要的線程調用的方法,在這種情況下MainLoop返回一個值。 – Bharath

+2

對不起,我是失眠症,我討厭睡眠():-) –

+0

使用線程是一個更清潔的方式,因爲你不會用定時器來暫停整個系統? – tmjam

相關問題