开发者

c# console app using thread

开发者 https://www.devze.com 2023-04-05 22:28 出处:网络
I have a console app. I need to implement a do while that loop infinitely and a thread that at every 3 seconds returns a list of items from a page. How can I do that? I have a methold called getId( st

I have a console app. I need to implement a do while that loop infinitely and a thread that at every 3 seconds returns a list of items from a page. How can I do that? I have a methold called getId( string URL) . how do I implement the thread in the do w开发者_JAVA技巧hile?


Using System.Timers.Timer class:

string url = "www";            
System.Timers.Timer timer = new System.Timers.Timer(3000);
timer.Elapsed += (o, e) => this.GetId(url);
timer.Start();

Timer is designed for use with worker threads in a multithreaded environment. Server timers can move among threads to handle the raised Elapsed event, resulting in more accuracy than Windows timers in raising the event on time.

The Timer component raises the Elapsed event, based on the value of the Interval property


I would not use a timer - what happens if the item retrieval takes longer than three seconds?

Can you live with a sleep(3000) loop?

Rgds, Martin

0

精彩评论

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