Solved

How can I monitor a process

Posted on 2010-11-12
3
374 Views
Last Modified: 2012-05-10
We have built an application in vb.net 2010 which imports data records to QuickBooks. From our own application, we are invoking a third party
application thru command shell. There are two possible scenarios once we invoke the third party application as below:

Dim p As New System.Diagnostics.Process()
p.StartInfo.FileName = "Name of the executable"
p.StartInfo.Arguments = "paramters list"
p.Start()

1) The third party application launches fine, it goes ahead and produces a log file.
2) The third party application does not launch correctly due to the wrong command line parameters or due to some other reasons
   In this case, the third party application displays a message box with a specific information - we need to grab that error message


We need to know the status of the third party application - if it is processing the records or it has displayed the error message. In both cases,
the process is running, has not exited and is responding. Is there any API call that we can use to determine the state of the process? Can we use the handle of the process to know the state?
Can we use Windows messages to grab the information displayed in the message box?
0
Comment
Question by:toddj1
3 Comments
 
LVL 3

Assisted Solution

by:sufianmehmood
sufianmehmood earned 83 total points
ID: 34122998
there are two things you can do here......
first launch a thread that should monitor if the exe is running or not, if the third party application is running, use HOOKS to grab the error message window if any and get the error message.
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 83 total points
ID: 34123436
0
 
LVL 15

Accepted Solution

by:
Russell_Venable earned 84 total points
ID: 34129426
You can monitor your 3rd part program by adding Input, output, and Error EventHandlers then redirect to a messagebox or textbox for display. Such as this.




Process p = new Process();
            //Create Startinfo
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = "3rdpartyprocess.exe";
            info.Arguments = "";
            info.RedirectStandardInput = true;
            info.RedirectStandardError = true;
            info.UseShellExecute = false;

            // Add output and error handling
            p.ErrorDataReceived += new DataReceivedEventHandler(ProcessErrorHandler);
            p.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);

            p.EnableRaisingEvents = true;
            p.StartInfo = info;
            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
            p.WaitForExit();

            using (StreamWriter sw = new StreamWriter(p.StandardInput.ToString()))
            {
                if (sw.BaseStream.CanWrite)
                {
                    sw.WriteLine("Your Commands");
                    sw.WriteLine("Go");
                    sw.WriteLine("Here");
                }
            }
        }

        static void ProcessErrorHandler(object sender, DataReceivedEventArgs e)
        {
            // Ouput input / output to a txtbox or Messagebox using e.Data.
            string strMessage = e.Data;
            if (richTextBox.Text != null && !String.Empty(strMessage))
            {
                App.Instance.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate()
                {
                    Paragraph para = new Paragraph(new Run(strMessage));
                    para.Margin = new Thickness(0);
                    para.Background = brushErrorBrush;
                    box.Document.Blocks.Add(para);
                });
            }

            // or

            Console.WriteLine("Received from standard out: " + e.Data);
        }

        static void ProcessOutputHandler(object sender, DataReceivedEventArgs e)
        {
            string strMessage = e.Data;
            if (richTextBox.Text != null && !String.Empty(strMessage))
            {
                App.Instance.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate()
                {
                    Paragraph para = new Paragraph(new Run(strMessage));
                    para.Margin = new Thickness(0);
                    para.Background = brushErrorBrush;
                    box.Document.Blocks.Add(para);
                });
            }

            // or

            Console.WriteLine("Received from standard error: " + e.Data);
        } 
        
        // Etc etc.
        //
    }

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Capture logon name 13 73
VB.NET Repostiory Pattern 7 36
VB.NET 2008 Populate DataModel with DataTable 44 28
RLDC Reporting in Visual studio 11 15
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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