Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I monitor a process

Posted on 2010-11-12
3
Medium Priority
?
378 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 332 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 332 total points
ID: 34123436
0
 
LVL 15

Accepted Solution

by:
Russell_Venable earned 336 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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