Solved

threading lockup

Posted on 2004-08-30
2
202 Views
Last Modified: 2010-04-15
I have a main form appliction that  creates a threadon in it's Load() function
myThread=newThread(new ThreadStart(myThreadFunction);
myThread.Start();

The thread creates a tcpListener objects and creates another form on connection
new tcpipObject(myListener.AcceptSocket(),this)

tcpipObject is a System.Windows.Forms.Form that creates another thread in it's constructor
secondThread = new Thread(new ThreadStart(secondThreadFunction);
secondThread.Start();
this.Show();

In secondThreadFunction() I have a view only multiline text box named textboxStatus
textBoxStatus.Text += "Connection Made\r\n";

using the debugger I get to this line, but an F10 does not move to the next line.

Is there something ovbious that I am doing incorrectly?
Thank You
0
Comment
Question by:PhilC
  • 2
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 11930047
Any thread which creates Windows forms must have it's own message loop. Worker thread created in main form Load finction doesn't have message loop and cannot create Windows forms. See additional information in the Application.Run Method MSDN topic:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsapplicationclassruntopic.asp

You can try to create message loop in the worker thread calling Application.Run Method from it:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsapplicationclassruntopic1.asp

But I beleive the better way is to create all forms in the main thread. When you need to create additional form, call main form method using BeginInvoke, and create new form in this method. BeginInvoke and Invoke are always called from the thread which created the form. Any call from the worker thread to the main thread must be done using BeginInvoke or Invoke.

See also:
http://www.codeguru.com/Csharp/Csharp/cs_syntax/article.php/c5851/
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 11930073
By the way, all .NET networking classes may work in asynchronous mode, allowing to write single-threaded applications. All required worker threads are created internally, and callback functions are always called in the main thread.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now