Solved

How can I monitor a process

Posted on 2010-11-12
3
376 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…

751 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