Re: Async delegates, illegal cross thread UI control access
Ron <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <2BB7FDC897CD477DA396D622CB6C967F@pandora> |
Yeah, to be sure I set CheckForIllegal.... to true in Form_Load. I remember when I starting VS2005 and did some async calls which ultimately updated the UI, that I got that warning and thought that was pretty conveniant, and it taught me the correct way to do it. But now, I can't get that warning back. I'm almost sure that if I attempted the same functionality using a Thread versus async delegates, that I'd raise the error. Ron ----- Original Message ----- From: "Peter Ritchie" <[email protected]> To: <[email protected]> Sent: Thursday, March 29, 2007 2:02 PM Subject: Re: [DOTNET-WINFORMS] Async delegates, illegal cross thread UI control access > 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. >