2013-04-10 98 views
1

我需要通过以下int dldnow sendData静态方法/委托。System.Threading.Timer TimerCallback委托与参数?

public int dldnow; 
Timer timer = new Timer(new TimerCallback(sendData), null, 1000*30, 1000*30); 

public static void sendData(object obj) 
{ 
    string imageCount = (string)dldnow; 
    string imageCountJson = wc.DownloadString("http://*********/u.php?count=" + imageCount); 
} 
+1

正如答案中所述,只需将'dldnow'作为参数'state'(现在你写'null')。另一件事:你不需要完全写入新的TimerCallback(sendData)。从C#2开始,您只需编写'sendData'(这称为方法组转换)。 – 2013-04-10 16:19:32

+0

正确的版本参数刷新http://stackoverflow.com/questions/15931850/timer-callback-delegate-doesnt-get-the-refreshed-value-from-parameter-on-every – 2013-04-10 17:04:57

回答

5

如果你想一次通过它,使用第二个构造函数参数:

System.Threading.Timer(new TimerCallback(sendData), dldnow, 1000*30, 1000*30); 

如果你要经常访问它,你可以做一个静态挥发场:

public static volatile int dldnow; 

(volatile需要使它从多个线程访问时总是最新的)