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
jtran007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ambienceCommented:
USe a delegate to the lengthy task function, like
http://msdn.microsoft.com/en-us/library/system.iasyncresult.aspx 
 
0
jtran007Author Commented:
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
0
ambienceCommented:
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?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

jtran007Author Commented:
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

0
ambienceCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jtran007Author Commented:
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
0
ambienceCommented:
Can show your code please? The section where you launch the task and your Cancel handler part.
0
jtran007Author Commented:
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());
            }
0
jtran007Author Commented:
Thanks,
JT
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.