Threading in a form...

Tech-Archive recommends: Speed Up your PC by fixing your registry



Hi,

I'm trying to get my head round Async. procedure calls.

I have the following code:

delegate double GetLabourWIPDelegate(DateTime reqDate);
....
GetLabourWIPDelegate getLabourWIPDelegate = new
GetLabourWIPDelegate(GetLabourWIPOnDate);
AsyncCallback ac = new AsyncCallback(DoResult);
IAsyncResult ar = getLabourWIPDelegate.BeginInvoke(dteToDate.Value,ac,null);
....
private void DoResult(IAsyncResult ar)
{
GetLabourWIPDelegate getLabourWIPDelegate =
(GetLabourWIPDelegate)((AsyncResult)ar).AsyncDelegate;
double result = getLabourWIPDelegate.EndInvoke(ar);
lblWIPLabourTo.Text = result.ToString("###,###,###,##0.00");
}

This seems to work, it starts a new thread that async. does the calculation
on the date in dteToDate ( calls GetLabourWIPOnDate), then calls DoResult on
completion which sets the label on the form to the result of the
calculation. Is is correct though?

My other question is, what happens if I want to do another calculation at
the same time. I will be calling the same calculation method, but using a
different date value, and putting the result into a different label. How
would I alter my code to do this?

Hope this makes sense, if not, please ask me to clarify anything.

Thanks,

Chris.


.



Relevant Pages

  • Re: Threading in a form...
    ... I usually pass in the delegate reference as the state argument so you can ... Best practices for event-based async pattern on MSDN: ... GetLabourWIPDelegate getLabourWIPDelegate = new ... calculation on the date in dteToDate, ...
    (microsoft.public.dotnet.general)
  • Re: Threading in a form...
    ... Only one thing wrong with your code;-)) you called your Delegate ... you cannot access controls from the callback method because it ... label you're wanting to modify. ... GetLabourWIPDelegate delegate = ...
    (microsoft.public.dotnet.general)
  • Re: Threading in a form...
    ... you cannot access controls from the callback method because it ... label you're wanting to modify. ... calling BeginInvoke on the delegate. ... GetLabourWIPDelegate delegate = ...
    (microsoft.public.dotnet.general)