C # timer functions?

I work in C # 2010 and I have a timer:

private Timer m_Timer;


void KA(string SendStuff, object State)
{

}

m_Timer = new Timer(new TimerCallback(KA(DATA)));

I want this timer to call the "KA" function, passing it "DATA".

DATA is a string

How to transfer timer information to the "KA" function?

thank

+5
source share
3 answers

you can use delegate / lambda expression:

string stuff = "hi";
Timer t = new Timer(new TimerCallback(_ => KA(stuff, null)));

Edit:

After consideration and feedback, a cleaner and simplified version:

string stuff = "hi";
Timer t = new Timer(state => KA(stuff, state));
+8
source

I think this is what you are looking for

private Timer m_Timer;

void KA(object state)
{
    string data = (string) state;
}

m_Timer = new Timer(new TimerCallback(KA), DATA);
+4
source

, # , , .

:

IPoller poller = new UrlPoller(args[0], TimeSpan.FromSeconds(7));
IPolling pollingComponent = new Polling.Core.Polling(poller);
pollingComponent.SubscribeForPollingUpdates(PollingAction);
pollingComponent.Start();

.

http://www.avantprime.com/blog/24/an-example-of-repeating-code-using-a-worker-thread-without-using-timers-c

0
source

All Articles