I have to implement very complex algorithm with a lot of iterations, matrix operations etc. There are two major loops for Fourier series approximation. I would like to know what is the best approach to implement progress callback. I na future I would like to use this algorithm in WPF app and I would like to implement progress bar. How to prepare algorithm to make progress bar implementaion easy in a future?
I am thinking about something like this:
static void Main(string[] args)
{
Console.Write("Progres... ");
Alg((i) => UpdateProgress(i));
}
public static void UpdateProgress(int iteration)
{
string anim = @"|/-\-";
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.Write(anim[iteration%5]);
}
public static void Alg(Action<int> progressCallback)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(50);
progressCallback(i);
}
}
If you prefer to use
TPL
, why not stick with it? You can use 'IProgress' http://msdn.microsoft.com/pl-pl/library/hh193692.aspx .You need object which implements interface INotifyPropertChanged. The property of this object that represents progress state will be bound to XAML element that visualize that progress state (Progressbar). In your algorithm you need just set this property to appropriate value. Your algorithm can get this object as parameter. Or you can set this property in delegate which is supplied to you algorithm. WPF takes care about delivering change of property to UI thread if you are working with multiple threads.