开发者

C# Timer Functions?

I am working in C# 2010 and I have a Timer:

priva开发者_JS百科te Timer m_Timer;


void KA(string SendStuff, object State)
{

}

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

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

DATA is a string

How would I pass using a timer information to the function "KA"?

Thanks


you can use a 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 is:

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


I think this is what you're looking for

private Timer m_Timer;

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

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


Try investigating this article that demonstrates in c# a generic polling component that runs at a specified interval and uses a background thread to perform the user action specified.

Sample usage:

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

For the code and complete sample see:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜