Solved

threading lockup

Posted on 2004-08-30
2
198 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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

8 Experts available now in Live!

Get 1:1 Help Now