473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Asynchronous Threading Help

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
Nov 20 '05 #1
4 2270
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" <sl*****@msn.com> wrote in message
news:d8**************************@posting.google.c om...
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

Nov 20 '05 #2
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" <sl*****@msn.com> wrote in message
news:d8**************************@posting.google.c om...
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

Nov 20 '05 #3
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
Nov 20 '05 #4
Hi, Pug - see below

"Pug Fugly" <sl*****@msn.com> wrote in message
news:d8**************************@posting.google.c om...
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
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sam Loveridge | last post by:
Hi all. I'm relatively new to delegates and asynchronous threading and am running into an issue. I need to asynchronously call a method (which I'm doing with a delegate and BeginInvoke) and from...
0
by: AndyNY | last post by:
I am trying to write a dll in C# using SAPI 5.1 SDK. The DLL is supposed to take in a prerecorded WAV file and provide both the recognized text and a wav file created from Text to speech on the...
2
by: trialproduct2004 | last post by:
Hi all, i am having c# application. I have bunch of URL's which i want to validate. that means i want to check whether those url's are valid or not. When i start processing url's one by on...
0
by: Pug Fugly | last post by:
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...
0
by: OpticTygre | last post by:
I've been reading some things about threading, delegates, threadpools, locks, etc... yet I can't quite seem to grasp some of the concepts on it quite yet. I'm currently working on a project I need...
3
by: Keith Mills | last post by:
Hello, please find attached a basic outline of what I am attempting to accomplish... basically I want to create a number of THREADS (which I can do fine), but I then need a method for them to be...
3
by: teillon | last post by:
I'm trying to write a VB.NET utility that performs a looping task--say, list all files in a directory, over and over again, continuously. I want to be able to put this logic in a User Control...
0
by: Kooshesh | last post by:
Hello, I have this loop in a function System.Threading.ThreadPool.SetMaxThreads(4, 4) For Each row As DataGridViewRow In DataGridView1.Rows If Not row.Cells(0).Value Is Nothing Then Dim...
1
by: slikrik98 | last post by:
Greetings, I believe I have narrowed down my issues to one simple question, and I'm hoping someone with async events experience can help me out. My question is: how is using...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.