Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

threading lockup

Posted on 2004-08-30
2
Medium Priority
?
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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