Results 1 to 5 of 5
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Vienna, Wien, Austria
    Posts
    5,009
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Capturing an Error Message (2.0)

    If you Shell a command line application, and the CL App rejects the input with an error message, a Normal View console screen will disappear before you've had time to read the error message. Pardon my ignorance, but how does one intercept the error message for display in the .NET Windows App?
    Gre

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Capturing an Error Message (2.0)

    You could use the Debug tools in .NET to capture this.

    The first thing I would recommend trying is to launch your application in Debug mode with a breakpoint set in the code at the desired location (prior to where you think the exception is being generated). Be sure to wrap the questionable areas of code in a Try/Catch block. In the Catch block, be sure to add a Debug.WriteLine statement to capture the exception to the Debug window.

    Do a search for Debug in the MSDN help and you'll find a wealth of information and walkthroughs.

    Hope this helps!

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Vienna, Wien, Austria
    Posts
    5,009
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Capturing an Error Message (VB 2.0)

    It's a virtual certainty that I phrased my original post too vaguely, I think. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    I'm working on a front end for certain existing third-party freeware console applications. The front-end itself is a standard Windows Application. The code in question is:<pre>HasProcessFinished(strActiveExe & strOptions)

    Private Sub HasProcessFinished(ByVal JobToDo As String)
    Dim proActive As Process
    Dim startInfo As New ProcessStartInfo(strActiveExe)

    startInfo.WindowStyle = CType(intView, ProcessWindowStyle)
    startInfo.Arguments = strOptions
    proActive = Process.Start(startInfo)

    While Not proActive.HasExited
    Threading.Thread.Sleep(100)
    End While

    End Sub</pre>

    If these were .NET console applications, then the error message would show in the Debug process. All that happens in stepping through is that the console screen flashes by (as ever). What I am seeking to do is: either capture the error message (if there is one) sent to the console screen or set a minimum time that the console window stays visible. (GC.StayAlive does not help in the latter instance.)

    Have I misunderstood your response?
    Gre

  4. #4
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Capturing an Error Message (VB 2.0)

    You can use the RedirectStandardOutput and RedirectStandardError properties of the StartInfo structure to point the StdOut and StdError of the process to Streams.

    For example, to return a list of file names from a Dir command you could do the following:

    Dim myProcess As Process = New Process

    myProcess.StartInfo.FileName="cmd.exe"
    myProcess.StartInfo.UseShellExecute = False
    myProcess.StartInfo.CreateNoWindow = True
    myProcess.STartInfo.RedirecteStandardInput = True
    myProcess.STartInfo.RedirecteStandardOutput = True
    myProcess.STartInfo.RedirecteStandardError = True

    myProcess.Start

    Dim sIn as StreamWriter = myProcess.StandardInput
    sIn.AutoFlush = True
    Dim sOut as StreamReader = myProcess.StandardOutput
    Dim sErr as StreamReader = myProcess.StandardError

    sIn.Write("Echo off" & System.Environment.NewLine)
    sIn.Write("dir" & System.Environment.NewLine)
    sIn.Write("Exit" & System.Environment.NewLine)

    Dim s As String = String.Empty

    Do

    s = sOut.ReadLine

    ' ..... Do what you want with the data here

    Loop Until s Is Nothing

    s = String.Empty

    Do

    ' ..... same for StandardError

    Loop Until s Is Nothing

    If Not myProcess.HasExited Then myProcess.Kill

    sIn.Close
    sOut.Close
    sErr.Close

    myProcess.Close


    I'm sure you can adapt this to trap the output from your program.

    Cheers,
    Kevin Bell

  5. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Vienna, Wien, Austria
    Posts
    5,009
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Capturing an Error Message (VB 2.0)

    Many thanks for shining through the fog. That looks just the ticket! <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>
    Gre

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •