Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

threading lockup

Posted on 2004-08-30
2
Medium Priority
?
225 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

782 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