473,699 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threading and label visibility problem

I stumbled across a weird problem that I don't seem to understand and was
wondering if anyone could help explain why it is occurring:

Basically I have set up a client/server application where the client and
server communicate via a NetworkStream. My problem is on the Client side.

The Client's "Main Thread" starts up a form (Form1) which then invokes a new
thread I'll label as "Listening_Thre ad". This thread's sole purpose is to
listen to any messages from the Server. So it's sitting on the following
blocking call while waiting from anything from the server:
IncomingMessage = DirectCast(objI Formatter.Deser ialize(objNetwo rkStream),
NetworkMessage)

My program sends a message to the server (on the "Main Thread") requesting
permission to open a new Form on the Client. When the Server responds back
(on the "Listening Thread") with permission granted, that thread opens a new
form (Form2) which when created gets passed an object in the constructor
method that will initilize the form, something like:

Dim obj As New Form2(Initilizi ng_Object)

The (Form2) constructor will then call an initilizing method with the
Initilizing_Obj ect like so:

Public Sub New(ByVal Initilizing_Obj ect As Object)

InitForm(Initil izing_Object )

Create_Form2_Th read()

End Sub

Then the constructor will create a new thread "Form2_Thre ad" which calls the
method with the Me.ShowDialog call.

So what I have is the "Main Thread" which handles any GUI actions in (Form1)
and I have the "Listening_Thre ad" which is a child of the "Main"Threa d" and
is blocking while waiting for Server messages, and finally I have a
"Form2_thre ad" which is a child of the Listening_Threa d" which handles any
GUI actions in (Form2). [I'm not sure if that's the best way to set up the
threads I wanted, but since I haven't learned how to communicate between
threads, that is what I used]

Now if I make another request to the Server, it will respond back (on the
"Listening_Thre ad") to update some label objects on (Form2). What is
happening though, is that when I try to set a label object's Visibililty to
True (Label1.Visible = True), then (Form2) will hang.

What I discovered after much testing, is that when I called the InitForm()
method from the (Form2) constructor, it sets some of the Form's label object
fields to False (Label1.Visibil ity = False). [This needs to be done, because
the label objects are shown and hidden as part of the application flow]

When I comment out the (Label1.Visibil ity = False) line in the Initilizing
method, (Form2) no longer hangs. So the setting of the Visibiliy from False
to True is causing the hang in some way.

I next moved the Initilization of (Form2) outside of the constructor like
so:

Dim obj As New Form2()

obj.InitForm(In itilizing_Objec t)

And this made the whole problem go away - with the (Label1.Visibil ity =
False) line intact in the Initilizing method.

==> So now I'm total confused. I need to understand what this problem is.
Is it because the threading is set up somehow incorrectly, or because the
Form's constructor is doing something I don't understand, or is it a bug in
the .Net Framework, or perhaps the moon was just lined up incorrectly with
the rest of the planets?

If anybody can understand and explained what is occurring, I'd truly
appreciate it.

-- Mark




Nov 21 '05 #1
1 1449
Mark,

Your current design will not function correctly. This is because you
are attempting to host a form a thread without a message loop. To get
Form2 working correctly it needs to be hosted on the main GUI thread
(which is already running a message loop). You can accomplish this by
using the Invoke method on Form1. The Invoke method will marshal the
execution of any delegate onto the thread hosting the form or control
it was called on. It is also important to know that forms and controls
should only be accessed from the thread hosting them.

Brian

Mark Denardo wrote:

[snip]

So what I have is the "Main Thread" which handles any GUI actions in
(Form1) and I have the "Listening_Thre ad" which is a child of the
"Main"Threa d" and is blocking while waiting for Server messages, and
finally I have a "Form2_thre ad" which is a child of the
Listening_Threa d" which handles any GUI actions in (Form2). [I'm not
sure if that's the best way to set up the threads I wanted, but since
I haven't learned how to communicate between threads, that is what I
used]

[snip]

If anybody can understand and explained what is occurring, I'd truly
appreciate it.

-- Mark


Nov 21 '05 #2

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

Similar topics

2
30032
by: Xerxes | last post by:
Hi, can you tell me how I can make a <label> hidden? I have hidden the field after the label: var M_Hide = isNS4?'hide':'hidden'; var M_Show = isNS4?'show':'visible'; ..... <label id="uid">UserID: </label> <input name="UserID" type="text" value="" size="20"> <script language="Javascript1.2" type="text/javascript1.2">
3
539
by: Suhail Salman | last post by:
Dear All, i got the following error message in an applications that opens 13 threads and all of them calling a stored procedure which retreieves data from SQLServer and fills them to a table the error appears at the following statment sqlda.Fill(dtMessages); During an infinit loopp
4
10927
by: Gibs | last post by:
Hi, How can i make a label visible/invisible? I am able to do it for text box by using document.form1.TextBox1.style.visibility="hidden"; But when i am giving the same for the label, i am getting an error. any idea??
6
345
by: John Rugo | last post by:
Hi All, I have the most basic understanding of using Threading. I have no problem creating a thread, addressing it to a Sub Procedure, and Starting the Thread. Works Great! My problem is the following: I have: 1.A function that returns a Boolean 2.The Function calls different Sub Procedures on a variable numbered basis.
0
1482
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data into a background thread and get other data out and also update the main thread on which the main form was created. It seems to work fine. The basic function of the app is cheesy, I didn't spend any time on exception handling. northwind.mdb...
14
2930
by: Christian Kaiser | last post by:
We have a component that has no window. Well, no window in managed code - it uses a DLL which itself uses a window, and this is our problem! When the garbage collector runs and removes our component (created dynamically by, say, a button click, and then not referenced any more), the GC runs in a different thread, which prohibits the DLL to destroy its window, resulting in a GPF when the WndProc of that window is called - the code is gone...
5
4245
by: half.italian | last post by:
Hi all, I don't really understand how to properly use threading in my programs, however I have managed to get by so far using them improperly. Once again I have come up to what I think is something that won't work because of the way the program is set up. I have a long running process running underneath a gui in a different thread, and a progress bar that updates the status of the operation. What I would like to do is use something...
7
2374
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get moved, the progress window never does anything. Here is my code in full, if anybody who knows...
126
6698
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have obviously worked most of the kinks out of the generalized threading and processes idea (along with many...
0
8691
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
8620
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,...
1
8920
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
8887
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...
1
6536
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3060
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
2351
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2012
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.