CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2008
    Posts
    61

    Question How to keep track of background threads?

    I have two threads, one UI thread and one Background thread..

    I want to keep a track of the background thread so that I may kill my UI thread (a tray icon) when the background thread has finished...

    Please review my code for more details..

    Code:
    class Program
        {
            static void Main(string[] args)
            {
                Thread[] threads = new Thread[2];
                threads[0] = new Thread(new ThreadStart(UIMethod));
                threads[1] = new Thread(new ThreadStart(BackGroundMethod));
                threads[0].SetApartmentState(ApartmentState.STA);
                threads[0].Start();
                threads[1].Start();
                /*What should I write here so that I know when BackGroundMethod has finished, I can kill my system tray icon*/
            }
            public static void UIMethod()
            {
                /*Generate the system tray*/
                   Tray obj = new Tray();	
    	obj.GenerateTrayIcon();
    	Application.Run();
            }
            public static void BackGroundMethod()
            {
                /*Do some heavy background work..*/
            }
        }



    Any suggestions with some code would be highly appreciated..

    Thanks.
    Last edited by somu0915; May 27th, 2008 at 08:07 AM.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How to keep track of background threads?

    Do you want it to terminate the process when it ends? Then you can simply call Application.Exit().
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Nov 2007
    Posts
    24

    Re: How to keep track of background threads?

    Well, one option is to use a BackgroundWorkerThread to execute the task that the second thread is to fullfill and then when the RunWorkerCompleted event is fired you can close down the app.

    Check out the msdn article for it, should give you a good start on it :

    http://msdn.microsoft.com/en-us/libr...undworker.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured