473,804 Members | 4,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Asynchronous Invoke and the UI thread (using delegates)

Hello,

After studying some of the available resouces
(
http://msdn.microsoft.com/msdnmag/is...s/default.aspx) I
can see that it is forbidden to modify the UI directly from a secondary
worker thread. What I was wondering was whether I could instantiate a form
within my secondary thread, get some user input and carry on until I
finished? Does this break any rules of good programming conduct?
Mar 3 '06 #1
7 3921
"brisers" <br*****@discus sions.microsoft .com> schrieb
Hello,

After studying some of the available resouces
(
http://msdn.microsoft.com/msdnmag/is...s/default.aspx)
I can see that it is forbidden to modify the UI directly from a
secondary worker thread. What I was wondering was whether I could
instantiate a form within my secondary thread, get some user input
and carry on until I finished? Does this break any rules of good
programming conduct?

That's possible. The only condition: The Form must not have any relation to
Forms created in other threads.
Armin

Mar 3 '06 #2
"Armin Zingler" <az*******@free net.de> schrieb
"brisers" <br*****@discus sions.microsoft .com> schrieb
Hello,

After studying some of the available resouces
(
http://msdn.microsoft.com/msdnmag/is...s/default.aspx)
I can see that it is forbidden to modify the UI directly from a
secondary worker thread. What I was wondering was whether I could
instantiate a form within my secondary thread, get some user input
and carry on until I finished? Does this break any rules of good
programming conduct?

That's possible. The only condition: The Form must not have any
relation to Forms created in other threads.

"no relation" does not mean, it mustn't have a reference, but there mustn't
be a child/parent/owner relation.

In the new thread you can write:
sub threadmain

application.run (new OtherForm)

end sub

The thread ends as soon as the Form is closed.
Armin

Mar 3 '06 #3
Add the following to your form and you can update its controls from another
thread:

CheckForIllegal CrossThreadCall s = False

"brisers" <br*****@discus sions.microsoft .com> wrote in message
news:E8******** *************** ***********@mic rosoft.com...
Hello,

After studying some of the available resouces
(
http://msdn.microsoft.com/msdnmag/is...s/default.aspx) I
can see that it is forbidden to modify the UI directly from a secondary
worker thread. What I was wondering was whether I could instantiate a form
within my secondary thread, get some user input and carry on until I
finished? Does this break any rules of good programming conduct?


Mar 6 '06 #4
"Prester John" <pr*********@co mcast.net> schrieb
Add the following to your form and you can update its controls from
another thread:

CheckForIllegal CrossThreadCall s = False

This doesn't solve the problem. It only makes the runtime ignores faults.
Armin
Mar 6 '06 #5
Armin,

No, I don't think it breaks any rules of programming conduct. The code
you provided will work fine provided that you understand that OtherForm
should remain completely independent from the other forms of your
application running on the main UI thread.

However, without knowning exactly what you're wanting to do I question
the need for another UI thread. Can't you collect all of the inputs
your worker thread needs before you start it? That way the worker
thread doesn't need any user interaction. The worker thread can use
Control.Invoke to marshal the execution of a delegate onto the main UI
thread as needed to communicate back to forms inside your application.

Brian

Armin Zingler wrote:
"no relation" does not mean, it mustn't have a reference, but there mustn't
be a child/parent/owner relation.

In the new thread you can write:
sub threadmain

application.run (new OtherForm)

end sub

The thread ends as soon as the Form is closed.
Armin


Mar 6 '06 #6
"Brian Gideon" <br*********@ya hoo.com> schrieb
Armin,

No, I don't think it breaks any rules of programming conduct. The
code you provided will work fine provided that you understand that
OtherForm should remain completely independent from the other forms
of your application running on the main UI thread.
That's what I wrote.
However, without knowning exactly what you're wanting to do


*I* don't want to do anything. :-)
Armin
Mar 6 '06 #7
Yeah, somehow I totally confused you with the OP. Sorry about that.
My post was not directed at you :)

Brian

Armin Zingler wrote:
"Brian Gideon" <br*********@ya hoo.com> schrieb
Armin,

No, I don't think it breaks any rules of programming conduct. The
code you provided will work fine provided that you understand that
OtherForm should remain completely independent from the other forms
of your application running on the main UI thread.


That's what I wrote.
However, without knowning exactly what you're wanting to do


*I* don't want to do anything. :-)
Armin


Mar 6 '06 #8

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

Similar topics

4
1568
by: Chris | last post by:
Hello, With asynchronous programming : Why does the callback-function (running in a worker thread) may not update the state of a control on the main-form ? The docs say that you must use a MethodInvoker instead as in following example :
2
1591
by: Ingo Schasiepen | last post by:
Hi there, i'm evaluating if c# is suited to replace a script language. Most of the elements of this language can be replaced with a c#-library, but it also has some realtime-like elements. E.g., in this language, the following code is possible: { Int32 i=0; OnEvent ( ... ) {
4
2198
by: Coot | last post by:
I've been running many tests of the Asynchronous Delegate technique and I find that although BeginInvoke() does queue the method delegate onto a worker thread, it always does so on the _same_ thread. So if I call BeginInvoke() three times in a row, the method delegates are queued to the same thread and the second method doesn't begin until the first method completes, and the third doesn't begin until the second completes. This occurs...
4
4222
by: theBoringCoder | last post by:
Hello All, I just started learning about Delegates, and was wondering if someone could point me at some useful, straightforward examples of how to use delegates to make method calls asynchronously. I've been told that this is possible, and works similar to callbacks in C++, but cannot find any straightforward examples in C#. Thanks,
1
2843
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from remote server and display this info on the UI. From doing some research, I know that the way my implementation works today is not thread-safe, because essentially my worker thread updates the UI, which is BAD. So, here I am trying to figure out how...
1
1249
by: Tobias Matzat | last post by:
Hi! Is it possible to wait for the callback method to finish? When using a WaitHandle only the call of EndInvoke is awaited. I would like to do some calculations in the callback-method. But I don't know how to inform the main thread that the calculations are done, besides defining an event. Is it not possible to let the callback method do the work? Or am I getting something wrong? THX!
4
3585
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is quite tiresome to have to write code to call MyControl.Invoke for each control on the form, along with the delegates that are required for each. Is there a better way to do this? What I mean is, if I could marshal the
7
9719
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason for this being to stop the user thinking the application has frozen when in fact it is just waiting for a long SP to complete. Another reason for doing it like this is that I also have had a problem in the past where the SP takes longer than the...
4
2212
by: 6954 | last post by:
Hi! i need to implement some asynchronous call to my com+ component, but i need it to return some values (e.g. results of sql select statement). obviously queued components and MSMQ are out of the question... anyone has any ideas how to implement it? or just a guideline maybe? thank you
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
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
10319
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
10076
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
6851
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
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
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
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.