Re: Async delegates, illegal cross thread UI control access
Peter Ritchie <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <LISTSERV%[email protected]> |
That sounds odd. IIRC, There's really only three instances where you wouldn't get the cross-thread exception: the control hasn't fully been created yet, CheckForIllegalCrossThreadCalls is set to false, or the framework thinks your calling a thread-safe call. On Thu, 29 Mar 2007 13:49:51 -0500, Ron <[email protected]> wrote: >Hi all, > >I've encountered a situation where I thought VS2005 would warn me about a >illegal cross thread UI call. I actually wanted the error to happen because >I'm doing a demo for my peers on the Asynchronous Programming Model. > >WinForms app, long running web service call of 3-5 seconds. Don't want to >synchronously invoke the service call, and not using the asynchronous >methods in the proxy yet. > >The app is demonstrating the Asynchronous Programming Model ala MSDN docs to >my peers. > >Step 1. Create a delegate to match the service call. > >delegate Image GetMapImageDelegate(); > >Step 2. Create a delegate instance, invoke it > >void btnGetMap_Click() >{ > GetMapImageDelegate del = new GetMapImageDelegate(GetMapImage); > del.BeginInvoke(OnGetMapImageComplete, del); >} > >Image GetMapImage() >{ > // long running service call here > // return image; >} > >Step 3. In the callback, end the async invocation, getting the results > >void OnGetMapImageComplete(IAsyncResult ar) >{ > GetMapImageDelegate del = ar.AsycnState as GetMapImageDelegate; > Image image = del.EndInvoke(ar); > > // i expect this to cause the cross-thread error > pictureBox.Image = image; >} > >My understanding is that the thread in OnGetMapImageComplete is a thread >pool thread, and therefore accessing the pictureBox control should be >illegal. I traced the Thread.CurrentThread.ManagedThreadID in each method, >and showed that there are two different threads: btnGetMap_Click had ID 10, >all others were 11. > >I was all prepared to create yet another delegate to handle displaying the >image with the check for InvokeRequired pattern, but if the error (cross >thread access to UI control) doesn't exist, then...well it isnt' necessary >to do that. > >Any explanation as to why this is not occuring is appreciated.