3

I need to pass the following int dldnow to sendData static method / 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); } 
2
  • 1
    As stated in the answer, simply put in dldnow as the parameter state (where you write null now). Another thing: You don't have to write new TimerCallback(sendData) in full. Since C# 2 you can just write sendData (this is called method group conversion). Commented Apr 10, 2013 at 16:19
  • Right version with parameter refreshing stackoverflow.com/questions/15931850/… Commented Apr 10, 2013 at 17:04

1 Answer 1

10

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:

public static volatile int dldnow; 

(volatile is need so that it would be always up to date when accessed from multiple threads)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.