我需要将以下 int dldnow
传递给 sendData 静态方法/委托(delegate)。
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);
}
最佳答案
如果你想传递一次,使用第二个构造函数参数:
System.Threading.Timer(new TimerCallback(sendData), dldnow, 1000*30, 1000*30);
如果你想定期访问它,你可以做一个静态的volatile字段:
public static volatile int dldnow;
(需要 volatile 以便在从多个线程访问时它始终是最新的)
关于c# - 带参数的 System.Threading.Timer TimerCallback 委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15930350/