Link to home
Start Free TrialLog in
Avatar of jtran007
jtran007

asked on

Button dispatcher

Hi Expert,
I have a lenghtly calculation, how can I use button called Cancel to cancel this
long operation? I am using VS2010, and Task to perform a long calculation.
Please advise.
Thanks,
JT
Avatar of ambience
ambience
Flag of Pakistan image

USe a delegate to the lengthy task function, like
http://msdn.microsoft.com/en-us/library/system.iasyncresult.aspx 
 
Avatar of jtran007
jtran007

ASKER

Hi Expert,

What I try to achieve while a "bakcground " task is running, I use the dispatcher from
one of a button to cancel this task.
How can I do it?
Thanks,
JT
Ok, what sort of background task do you have?
A thread or an asynchrounous delegate? Also do you have control over the code for the task, i.e. can you modify it if needed?
Hi expert,
I use Parallel task to perform my calculation eg. Task task = Task.Factory.StartNew(() => s.CalculatePerformance(iLocal, token)..

s is an object with its behaviour CalculatePerformance that calculate a lot of things.
I have full control of this task.
I'd like to be able to cancel this task if it takes too long.
Thanks,
JT

ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
HI Expert,

I did use this Cancellation method; however, when my task running, I could not
click the cancel button so that cancellation token can be activated.
So do you know how to invoke button action?
Thanks,
JT
Can show your code please? The section where you launch the task and your Cancel handler part.
Hi Expert,

Attached is the code:

                 Task.Factory.StartNew(() =>
                 {
                     while (!token.IsCancellationRequested)
                     {
                         s.ThreadPoolCallback(iLocal);
                         //task.Wait();
                         if (s.CallsAll > 0)
                             statArray[iLocal] = s;
 
                         BeginInvoke((System.Action)(() =>
                         {
                             #region UI

                             if (s.CallsAll > 0)
                             {
                                 dgViewResult_P.Visible = true;
                                 displayLabel = true;
                                 lblPer.Visible = true;
                                 DataRow rowNew = ds.Tables["Stat"].NewRow();
                                 rowNew["Dialcode"] = Dialcodes;
                                 rowNew["Carrier"] = s.CarrierName;
                                 rowNew["Calls(#)"] = s.CallsAll.ToString();
                                 // rowNew["Duration (mm:ss)"] = s[m].ss_duration30S;
                                 if (s.ss_duration30S.Equals("NaN"))
                                     rowNew["Duration (mm:ss)"] = "0";
                                 else if (s.ss_duration30S.Equals(""))
                                     rowNew["Duration (mm:ss)"] = "0";
                                 else
                                     rowNew["Duration (mm:ss)"] = s.ss_duration30S;

                                 //rowNew["Duration (min:sec)"] = s[m].Duration30s;
                                 if (s.Acd.ToString().Equals("NaN"))
                                     rowNew["ACD(sec/#)"] = "0";
                                 else
                                     rowNew["ACD(sec/#)"] = s.Acd.ToString();
                                 if (s.Asr.ToString().Equals("NaN"))
                                     rowNew["ASR(%)"] = "0";
                                 else
                                     rowNew["ASR(%)"] = s.Asr.ToString();
                                 if (s.Calls30S.ToString().Equals("NaN"))
                                     rowNew["ASR30(%)"] = "0";
                                 else
                                     rowNew["ASR30(%)"] = s.Asr30S.ToString();

                                 ds.Tables["Stat"].Rows.Add(rowNew);
                                 DataView dv = ds.Tables[0].DefaultView;
                                 this.dgViewResult_P.DataSource = dv;
                             }
                             #endregion UI
                         }));

                         break;
                         // task.Wait();
                     }

                 }, token).ContinueWith(_ =>
                 {
                     _cancellation = null;

                     lblPerformanceRecord.Visible = true;
                     txtPerformanceRecord.Visible = true;
                     btnPerformanceRecord.Visible = true;

                     if (!displayLabel)
                     {
                         lblPer.Visible = false;
                         WarningMessage("No calls for this code: " + Dialcodes);
                         lblPerformanceRecord.Visible = false;
                         txtPerformanceRecord.Visible = false;
                         btnPerformanceRecord.Visible = false;
                         //buttonStart.Enabled = true;
                         //return;
                     }                
                 }, TaskScheduler.FromCurrentSynchronizationContext());
            }
Thanks,
JT