I had an old winform app which used a performant plotting component (Mitov.PlotLab) in a timer like this:
private void updateGuiTimer_Tick(...)
{
doSomeDemandingCalculations();
plotTheResultsInComponent();
}
I did some refactoring and changed the code like this:
Task.Factory.StartNew(()=>
{
while(isRunning)
{
doSomeDemandingCalculations();
if(isPlotting) continue;
isPlotting = true;
BeginInvoke(new Action(()=>
{
plotTheResultsInComponent();
isPlotting = false;
}));
}
}, TaskCreationOptions.LongRunning);
The problem is that the old approach could keep up with tick interval of 50 ms, and new code does just 20 percent of plotting compared to old one.
- I replaced
BeginInvoke
withInvoke
with no luck - I saw This and This which didn't help.
- I replaced single continuous Task with repetitive Task launches and this made performance even worse.
Didn't I offload the computation from UI thread to another one and just did the plotting in UI thread?
There where 3 timers in old code which did similar work. I refactored all of them to Task
s.
Is there something wrong with my approach?