Reputation: 15138
I have a job-application which does regular imports / exports every X minutes.
Some Imports needs time (e.g. 60 seconds) and to avoid freezing my complete application (I have a log-screen within to see what happens), I implemented Threading to my Worker:
/// <summary>
/// Eigentlicher JobTimer Tick, schmeißt den Worker jeden Tick neu an.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void JobTimer_Tick(object sender, EventArgs e)
{
WriteLog("Jobtimer Tick");
JobTimer.Enabled = false;
ThreadPool.QueueUserWorkItem(StartWorker);
}
/// <summary>
/// StartWorker Wrapper for Threading
/// </summary>
/// <param name="state"></param>
private void StartWorker(object state)
{
Worker w = new Worker();
w.StartWork(); //<--- Jobs will be Done here
JobTimer.Enabled = true; //<---Problem here
}
My Problem is, I have to disable the JobTimer (which ticks every 1000ms to see if there are jobs todo) so the JobTimer doesn't get overruned by jobs that are currently running. But I can't enable him again, because it seems the JobTimer on the threaded method is not the same as on the call method...
So how can I get notified when my Async ThreadPool is finished?
Upvotes: 0
Views: 341
Reputation: 120450
The new task-based async/await features make expressing this much easier. How about sacking the timer and restating your problem using tasks:
So:
public async Task RunWorkLoop(CancellationToken token)
{
while(!token.IsCancellationRequested)
{
//runs in ThreadPool, doesn't complete until StartWork is done
await Task.Run(() => new Worker().StartWork());
await Task.Delay(someTimeInterval);
}
}
Upvotes: 3