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
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
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
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?
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?
ASKER
Hi expert,
I use Parallel task to perform my calculation eg. Task task = Task.Factory.StartNew(() => s.CalculatePerformance(iLo cal, 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
I use Parallel task to perform my calculation eg. Task task = Task.Factory.StartNew(() => s.CalculatePerformance(iLo
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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.
ASKER
Hi Expert,
Attached is the code:
Task.Factory.StartNew(() =>
{
while (!token.IsCancellationRequ ested)
{
s.ThreadPoolCallback(iLoca l);
//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().Equ als("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.DataSo urce = dv;
}
#endregion UI
}));
break;
// task.Wait();
}
}, token).ContinueWith(_ =>
{
_cancellation = null;
lblPerformanceRecord.Visib le = true;
txtPerformanceRecord.Visib le = true;
btnPerformanceRecord.Visib le = true;
if (!displayLabel)
{
lblPer.Visible = false;
WarningMessage("No calls for this code: " + Dialcodes);
lblPerformanceRecord.Visib le = false;
txtPerformanceRecord.Visib le = false;
btnPerformanceRecord.Visib le = false;
//buttonStart.Enabled = true;
//return;
}
}, TaskScheduler.FromCurrentS ynchroniza tionContex t());
}
Attached is the code:
Task.Factory.StartNew(() =>
{
while (!token.IsCancellationRequ
{
s.ThreadPoolCallback(iLoca
//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("
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("
rowNew["ACD(sec/#)"] = "0";
else
rowNew["ACD(sec/#)"] = s.Acd.ToString();
if (s.Asr.ToString().Equals("
rowNew["ASR(%)"] = "0";
else
rowNew["ASR(%)"] = s.Asr.ToString();
if (s.Calls30S.ToString().Equ
rowNew["ASR30(%)"] = "0";
else
rowNew["ASR30(%)"] = s.Asr30S.ToString();
ds.Tables["Stat"].Rows.Add
DataView dv = ds.Tables[0].DefaultView;
this.dgViewResult_P.DataSo
}
#endregion UI
}));
break;
// task.Wait();
}
}, token).ContinueWith(_ =>
{
_cancellation = null;
lblPerformanceRecord.Visib
txtPerformanceRecord.Visib
btnPerformanceRecord.Visib
if (!displayLabel)
{
lblPer.Visible = false;
WarningMessage("No calls for this code: " + Dialcodes);
lblPerformanceRecord.Visib
txtPerformanceRecord.Visib
btnPerformanceRecord.Visib
//buttonStart.Enabled = true;
//return;
}
}, TaskScheduler.FromCurrentS
}
ASKER
Thanks,
JT
JT
http://msdn.microsoft.com/en-us/library/system.iasyncresult.aspx