473,668 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2293
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.BeginInvok e and form must
exist on UI thread.

I would suggest to trace threads using Console.WriteLi ne 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.co m> wrote in message
news:d8******** *************** ***@posting.goo gle.com...
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.BeginInvok e and form must
exist on UI thread.

I would suggest to trace threads using Console.WriteLi ne 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.co m> wrote in message
news:d8******** *************** ***@posting.goo gle.com...
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.BeginInvok e 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.co m> wrote in message
news:d8******** *************** ***@posting.goo gle.com...
Thank for your suggestion Alex. You were right...When you said "To return to UI thread you should use form.BeginInvok e 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.BeginInv oke

Even then - check if myForm.BeginInv oke executes delegate on UI thread or
not. Depending how you get to this point it might happen you have to do one
more myForm.BeginInv oke. 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
1778
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 the callback method, or at some point after the EndInvoke has been called to end the asynchronous operation I need to asynchronously call a different method. I really want to keep this event based and use delegates and callbacks rather than loop...
0
1288
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 result. I have all the functionality working other then one minor yet bothersome problem. I can not figure out how to get a result back from the Speech Recognition engine without utilizing the recogniton event on the recoContext. Which fires on...
2
1277
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 synchronysly its taking lots of time. So what i want is to split above task. I want to use threading in this case to make process faster. I want to created threads which are processing say 5 urls at a time. Like that i want to create some threads...
0
240
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 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...
0
897
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 a little threading help with. For this question's sake, I have 3 objects: 1) A main form where I search directories for certian files. 2) An 'UploadFiles' class which creates an SFTP connection and uploads files to a specific IP address. 3) A...
3
1516
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 able to communicate with each other, either through a message loop, or some other manner. I ALSO need to be able to CALL specific functions / subs WITHIN a thread, based on what another THREAD is doing... here is the attached code... WHEN I click...
3
2253
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 with a text box for the path to monitor and a list box to display the results. I want to be able to drop this control numerous times on a parent project form and have them all run asyncronysly. I am not a Threading expert, but I was assuming...
0
830
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 user As New myUser(row.Cells(0).Value, row.Cells(1).Value)
1
2518
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 EventsHelper.FireAsync() different from using EventsHelper.Fire() BUT having the listener's event handler spawn a new thread to do the work? In my program, I have a data feed coming in and I have a separate form that has a DataGridView on it. Each time a message...
0
8382
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.