protected virtual void OnMyTaskProgressChanged(MyTaskProgressChangedEventArgs e) { if (MyTaskProgressChanged != null) MyTaskProgressChanged(this, e); }
private void MyTaskWorker(string[] files, AsyncOperation async, MyAsyncContext asyncContext, out bool cancelled) { cancelled = false; for (int i = 0; i < files.Length; i++) { // a time consuming operation with a file (compression, encryption etc.) Thread.Sleep(1000); // compute progress int progressPercentage = 100 * (i + 1) / files.Length; // raise the progress changed event MyTaskProgressChangedEventArgs eArgs = new MyTaskProgressChangedEventArgs(progressPercentage, files[i], Id, null); async.Post(delegate(object e) { OnMyTaskProgressChanged((MyTaskProgressChangedEventArgs)e); }, eArgs); if (asyncContext.IsCancelling) { cancelled = true; return; } } }
static void task_MyTaskProgressChanged(object sender, MyTaskProgressChangedEventArgs e) { Console.WriteLine("[MyTask][{2}] Progress: {0} %, Current file: {1}", e.ProgressPercentage, e.CurrentFile, e.Id); }