Solved

How can I monitor a process

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now