user4648576
user4648576

Reputation:

How to call system.threading.timer on same thread?

Everytime I switch something on my form, I restart my thread by doing (to call whatever was switched):

Retriever.Dispose();
Retriever = new System.Threading.Timer(CallPictureBoxRetriever, null, 0, 300000);

The problem is that this is yet creating another thread and closing the previous!

I am asking how can I call CallPictureBoxRetriever(Object state) on the same thread created so therefore I do not have to always dispose/recreate a thread; rather have 1 thread.

Upvotes: 0

Views: 791

Answers (2)

Robert McKee
Robert McKee

Reputation: 21487

Personally, I would create a thread that waits on an autoresetevent with a timeout of 30 seconds. Then have your code set the autoresetevent when you want it to run immediately.

https://msdn.microsoft.com/en-us/library/system.threading.autoresetevent%28v=vs.110%29.aspx

Upvotes: 1

RagtimeWilly
RagtimeWilly

Reputation: 5445

Just reset the timer:

Retriever.Change(0, 300000); // reset to 300 seconds

First argument is dueTime:

Specify zero (0) to restart the timer immediately.

Second is period:

The time interval between invocations of the callback method specified when the Timer was constructed, in milliseconds. Specify Timeout.Infinite to disable periodic signaling.

Full docs here

Upvotes: 2

Related Questions