PC Review


Reply
Thread Tools Rate Thread

Asynchronous Threading Help

 
 
Pug Fugly
Guest
Posts: n/a
 
      20th May 2004
I am unable to call the .Show() method on a form that I have passed
through .BeginInvoke as the AsyncState parameter. I can get back the
form correctly after the .EndInvoke is called in the callback, but the
process still seems to be on a different thread even though the
..EndInvoke finished running. I get "Controls created on one thread
cannot be parented to a control on a different thread." Which I know
is not allowed, but I thought once .EndInvoke is done running I should
be back to the original thread. The form is a module variable so it
is not a scope problem. The only way I can get the form to show is if
I create a delegate for the .Show() and call that with .Invoke(). But
after that, I cannot bind to it's datagrid because I get the same
error as above. It seems like I never return back to the same thread,
or the threads are never aborting. What am I missing here?

Thanks,
Slavisa
 
Reply With Quote
 
 
 
 
AlexS
Guest
Posts: n/a
 
      20th May 2004
Hi, Pug

After EndInvoke finished running - where are you? Because you do EndInvoke
from BeginInvoke delegate you are definitely in some thread - not the UI
one. To return to UI thread you should use form.BeginInvoke and form must
exist on UI thread.

I would suggest to trace threads using Console.WriteLine or Debug.Print -
you will see that you try to update ui form on non-ui thread. That's why you
have problem.

HTH
Alex

"Pug Fugly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I am unable to call the .Show() method on a form that I have passed
> through .BeginInvoke as the AsyncState parameter. I can get back the
> form correctly after the .EndInvoke is called in the callback, but the
> process still seems to be on a different thread even though the
> .EndInvoke finished running. I get "Controls created on one thread
> cannot be parented to a control on a different thread." Which I know
> is not allowed, but I thought once .EndInvoke is done running I should
> be back to the original thread. The form is a module variable so it
> is not a scope problem. The only way I can get the form to show is if
> I create a delegate for the .Show() and call that with .Invoke(). But
> after that, I cannot bind to it's datagrid because I get the same
> error as above. It seems like I never return back to the same thread,
> or the threads are never aborting. What am I missing here?
>
> Thanks,
> Slavisa



 
Reply With Quote
 
Pug Fugly
Guest
Posts: n/a
 
      21st May 2004
Thank for your suggestion Alex. You were right. The entire callback
function is in a completely different thread than the function from
where I called my .BeginInvoke. When you said "To return to UI thread
you should use form.BeginInvoke and form must exist on UI thread,"
what function did you mean that I would call the .BeginInvoke for. I
thought that .BeginInvoke can only be called on delegates, not on
forms. The form is in the UI thread which is the same thread that the
..BeginInvoke is being called from. I just need to get back to that
thread after my .EndInvoke comes back.

Thanks,
Slavisa
 
Reply With Quote
 
AlexS
Guest
Posts: n/a
 
      21st May 2004
Hi, Pug - see below

"Pug Fugly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thank for your suggestion Alex. You were right...When you said "To return

to UI thread
> you should use form.BeginInvoke and form must exist on UI thread,"
> what function did you mean that I would call the .BeginInvoke for...


If you declared say myForm on UI thread and run it there initially, say Form
myForm=new MyForm() etc.
you use myForm.BeginInvoke

Even then - check if myForm.BeginInvoke executes delegate on UI thread or
not. Depending how you get to this point it might happen you have to do one
more myForm.BeginInvoke. So, you need to pass myForm reference to async
method - or set some event in async parameters.

Main lesson - don't assume, always check which thread are you on. Then you
will be able to sort this mess fairly quickly.

HTH
Alex


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Help needed with Asynchronous threading Stoitcho Goutsev \(100\) Microsoft Dot NET Framework Forms 0 18th Jan 2007 07:53 PM
Help needed with Asynchronous threading =?Utf-8?B?UGhpbGlw?= Microsoft Dot NET Framework Forms 0 18th Jan 2007 09:59 AM
Asynchronous Threading Issue Sam Loveridge Microsoft Dot NET 9 21st Jan 2005 06:19 AM
Asynchronous threading question Pug Fugly Microsoft VB .NET 1 18th May 2004 11:52 PM
Asynchronous Sockets and Threading Darren Microsoft C# .NET 1 12th Nov 2003 08:51 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 PM.