Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / MFC

The Practical Guide to Multithreading - Part 2

Rate me:
Please Sign up or sign in to vote.
4.96/5 (119 votes)
12 Apr 2010CPOL43 min read 149.9K   5K   317  
More of practical situations to use multithreading!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

// @MT2
using System.Threading;
using System.Diagnostics;
// @MT2
namespace ProcessWait_NET
{
    enum ThreadReplyCode
    {
        ProcessNotStarted,
        ProcessTerminated,
        UnknownError
    }
    public partial class Form1 : Form
    {
        // @MT2

        // Handle to created process. Used to Kill on user's request
        Process ExternalProcess;

        // For thread safety on 'Controls' we use String instead.
        String ExecutablePath;

        // The delegate that would be called from process-watcher thread        
        delegate void ThreadReplyDelegate(ThreadReplyCode nCode);
        ThreadReplyDelegate theDelegate;

        // Enable or disable set of controls
        // true = process running and thread is waiting for it.
        void EnableControls(bool bIsRunning)
        {
            txtExePath.Enabled = !bIsRunning;
            btnBrowse.Enabled = !bIsRunning;
            btnLaunch.Enabled = !bIsRunning;

            btnTerminate.Enabled = bIsRunning;
        }
        // @MT2

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            theDelegate = ThreadReply;

            // Default to Notepad.
            // Following code assumes that system directory is not 
            // ended with backslash, and it adds here.
            txtExePath.Text = Environment.SystemDirectory + "\\Notepad.exe";

            lblStatus.Text = "Specify the executable's path, and hit 'Launch Process'\nIt would then wait for created process.";

            EnableControls(false);
        }

        private void btnLaunch_Click(object sender, EventArgs e)
        {
            // No validations here...
            ExecutablePath = txtExePath.Text;
            
            
            // We pretend that process would be created, so we disable
            // controls, and modify the status text.
            EnableControls(true);
            lblStatus.Text = "Process launched. Waiting for the process to exit..." +
                             "\nYou can terminate the spawned process by clicking 'Terminate'" +
                             "\nEither way the waiting-thread would come to know!";

            new Thread(ProcessStarterAndWatcher).Start();
            
        }



        void ThreadReply(ThreadReplyCode nCode)
        {
            switch(nCode)
            {
                case ThreadReplyCode.ProcessNotStarted:
                    lblStatus.Text = "Could not spawn the process. Please make sure the executable path is correct.";
                    break;

                case ThreadReplyCode.ProcessTerminated:
                    lblStatus.Text = "The external process exited.\nYou can start another process.";
                    break;

                case ThreadReplyCode.UnknownError:
                    lblStatus.Text = "This message should not appear!\nUnknown error while creating/waiting for process";
                    break;
            }

            EnableControls(false);
        }

        // @MT2 
        void ProcessStarterAndWatcher()
        {
            try
            {
                ExternalProcess = Process.Start(txtExePath.Text);
            }
            catch(Exception)
            {
                Invoke(theDelegate, ThreadReplyCode.ProcessNotStarted);
                return;            
            }

            // This is special case...
            // The process created and terminated, or existing process was
            // launched. It may happen when you open document file (DOC, JPG etc)
            // instead of EXE itself. With EXE, it can also happen, though!
            if (ExternalProcess == null)
            {
                Invoke(theDelegate, ThreadReplyCode.UnknownError);
                return;
            }

            // Now wait for the process to exit/terminate
            // Wait indefinitely...
            ExternalProcess.WaitForExit();

            // Let the GUI-thread know
            Invoke(theDelegate, ThreadReplyCode.ProcessTerminated);            
        }

        private void btnTerminate_Click(object sender, EventArgs e)
        {
            // I'm not being decent here to ask for confirmation.
            // Just kill the process!

            // Don't care if process exits by itself before this function
            // does so. It doesn't harm anyone. So, not checking return value.
            // Also, it is not required to set status, as WaitForExit
            // would return, and would call delegate...

            // Final note: It will NOT terminate any process created by the 
            // process we created. For example, you can launch cmd.exe
            // and from the Command Prompt (cmd.exe) you launch Notepad.
            // In that case, if you terminate the process, it would only
            // terminate cmd.exe, NOT Notepad.exe
            ExternalProcess.Kill();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdOpen = new OpenFileDialog();
            fdOpen.DefaultExt = "EXE";
            fdOpen.Filter = "Executables (*.exe)|*.exe";

            
            if ( fdOpen.ShowDialog() == DialogResult.OK)
            {
                txtExePath.Text = fdOpen.FileName;
            }
        }
        // @MT2
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Started programming with GwBasic back in 1996 (Those lovely days!). Found the hidden talent!

Touched COBOL and Quick Basic for a while.

Finally learned C and C++ entirely on my own, and fell in love with C++, still in love! Began with Turbo C 2.0/3.0, then to VC6 for 4 years! Finally on VC2008/2010.

I enjoy programming, mostly the system programming, but the UI is always on top of MFC! Quite experienced on other environments and platforms, but I prefer Visual C++. Zeal to learn, and to share!

Comments and Discussions