Using C# 4.0, I would like to know a simple robust way to update the UI from within function foo() shown below. I need to update some textboxes that display the number of urls processed and the number of bad links found. The general approach I've taken on the async task is based on an approach I found here:
How to use HttpWebRequest (.NET) asynchronously?
Pseudo Code in my code-behind on the form:
1. populate a ConcurrentBag
2. Task.Factory.StartNew ( () =>
Parallel.ForEach( ConcurrentBag, (d) =>
{
MyAsyncTask(d);
}
And MyAsyncTask does:
<snip>
try
{
IAsyncResult result = myWebRequest.BeginGetResponse (
new AsyncCallback( foo, state);
ThreadPool.RegisterWaitForSingleObject(
result.AsyncWaitHandle,
new WaitOrTimerCallback(TimeoutCallback),
state,
(MSEC * 1000),
true
);
}
catch (Exception ex) {}
private void foo(IAsyncResult result)
{
RequestState state = (RequestState)result.AsyncState;
WebRequest request = state.webRequest;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
//update the UI ??
}