简体   繁体   English

System.Threading.Timer具有参数的TimerCallback委托?

[英]System.Threading.Timer TimerCallback delegate with parameters?

I need to pass the following int dldnow to sendData static method / delegate. 我需要将以下int dldnow给sendData静态方法/委托。

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);
}

If you want to pass it once, use a second constructor parameter: 如果要传递一次,请使用第二个构造函数参数:

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

If you want to access it regularly, you can make a static volatile field: 如果要定期访问它,可以创建一个静态的volatile字段:

public static volatile int dldnow;

(volatile is need so that it would be always up to date when accessed from multiple threads) (需要使用volatile,以便从多个线程访问时始终保持最新状态)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM