Solved

How can I monitor a process

Posted on 2010-11-12
3
377 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
[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
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 86

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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

624 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