A
Anthony Paul
Hello everyone,
I just ran into a problem today that I found rather weird but can't
think of a reason why it's occurring, so I'm putting it out here to
see if any gurus have a better understanding of what's going on.
I have a method that I use to facilitate asynchronous operations such
that it accepts two delegate parameters, one that gets executed by a
background thread via the ThreadPool and the other a delegate to call
on the UI thread once the background thread is done. Works great, no
problems, but then I ran into a situation where I wanted to execute
multiple work delegates instead of just one, and once they are all
done only then would the UI delegate get called. So I did that, and
here's my modified method :
protected void DoAsyncStuff(ThreadStart[] workDelegates, ThreadStart
uiDelegate)
{
int workers = workDelegates.Length;
foreach (ThreadStart workDelegate in workDelegates)
{
ThreadPool.QueueUserWorkItem
(
(WaitCallback)delegate
{
workDelegate();
if (uiDelegate != null)
{
if (Interlocked.Decrement(workers) == 0)
{
Dispatcher.BeginInvoke
(
(ThreadStart) delegate
{
uiDelegate();
},
System.Windows.Threading.DispatcherPriority.Normal
);
}
}
},
null
);
}
}
Now here's my method that uses it :
private void MyMethod()
{
int result1;
int result2;
DoAsyncStuff
(
new Delegate[]
{
delegate
{
result1 = Model.GetRandomNumber();
},
delegate
{
result2 = Model.GetRandomNumber();
}
},
delegate
{
// do something here with the results
}
);
}
Everything executes fine, but when both work delegates have finished
executing and the UI delegate executes, result2 is the only one that
has a value. result1 didn't get updated. If I reverse the order,
again, the first delegate doesn't seem to return a value, but the
second one does. Does anyone understand what's going on here?
Regards,
Anthony
I just ran into a problem today that I found rather weird but can't
think of a reason why it's occurring, so I'm putting it out here to
see if any gurus have a better understanding of what's going on.
I have a method that I use to facilitate asynchronous operations such
that it accepts two delegate parameters, one that gets executed by a
background thread via the ThreadPool and the other a delegate to call
on the UI thread once the background thread is done. Works great, no
problems, but then I ran into a situation where I wanted to execute
multiple work delegates instead of just one, and once they are all
done only then would the UI delegate get called. So I did that, and
here's my modified method :
protected void DoAsyncStuff(ThreadStart[] workDelegates, ThreadStart
uiDelegate)
{
int workers = workDelegates.Length;
foreach (ThreadStart workDelegate in workDelegates)
{
ThreadPool.QueueUserWorkItem
(
(WaitCallback)delegate
{
workDelegate();
if (uiDelegate != null)
{
if (Interlocked.Decrement(workers) == 0)
{
Dispatcher.BeginInvoke
(
(ThreadStart) delegate
{
uiDelegate();
},
System.Windows.Threading.DispatcherPriority.Normal
);
}
}
},
null
);
}
}
Now here's my method that uses it :
private void MyMethod()
{
int result1;
int result2;
DoAsyncStuff
(
new Delegate[]
{
delegate
{
result1 = Model.GetRandomNumber();
},
delegate
{
result2 = Model.GetRandomNumber();
}
},
delegate
{
// do something here with the results
}
);
}
Everything executes fine, but when both work delegates have finished
executing and the UI delegate executes, result2 is the only one that
has a value. result1 didn't get updated. If I reverse the order,
again, the first delegate doesn't seem to return a value, but the
second one does. Does anyone understand what's going on here?
Regards,
Anthony