Threading Application

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a C# application that works, with a main menu where the user can
choose different options that run, and then return to the menu. I want to
change it so that the user can starting running an option, then initiate
another menu option, so that both are running at the same time,
independently. As each one finished, that thread would end and the main menu
would remain. Right now my code is like this:

while(true){
MenuChoice=GetChoice();
if(MenuChoice=='A'){
ApplicationA();
}
}

If the user picked A, it runs application A, and then control returns to the
menu program.

In the code it should be something like this:

while(true){
MenuChoice=GetChoice();
if(MenuChoice=='A'){
if(StartThread()){
ApplicationA();
}
}
}

So that each time a menu choice is started, it is a new thread, and the main
menu thread keeps waiting for input from the user if they choose another
option.

The threads should be totally independent, so that I can have static
classes, etc, but they do not interact in any way.

Is there a way to do this? Thanks.
 
What kind of response is that? You have no idea what I know, or why I am
doing what I am doing. Perhaps you should do something useful, instead of
wasting people's time.
 
Hi Richard,

Based on my understanding, you want to use a background thread to process
the menu selection code so that it will not block the WinForm UI.

Yes, asynchronous operation requires a thread in the background for
processing the time-consuming task. You may code your StartThread() method
like this:

private static void ApplicationA()
{
System.Diagnostics.Process.Start("C:\\Windows\\notepad.exe");
}

public void StartThread()
{
System.Threading.Thread t=new System.Threading.Thread(new
System.Threading.ThreadStart(ApplicationA));
t.Start();
}

Note: after using StartThread() to launch a background thread for
processing ApplicationA() method, there is no need to call ApplicationA()
in the Menu click event handler. So you should modify like this:

while(true){
MenuChoice=GetChoice();
if(MenuChoice=='A'){
StartThread()
}
}

Another way of using background thread is using
ThreadPool.QueueUserWorkItem to reuse the .Net thread pool thread instead
of launching a new thread. You may get more information regarding thread
pool by searching ThreadPool in MSDN.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top