Results 1 to 4 of 4

Thread: Problem with ReadToEnd and output freeze

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Problem with ReadToEnd and output freeze

    Hello, I'm trying to embed command prompt to my app. The output textbox is freezing when I run the program. Actually it isn't freezing, it's waiting exe to finish its work. I want to get lines immediately as it does in command prompt.

    vb.net Code:
    1. Private Sub CMDAutomate()
    2.  
    3.     Dim cmdProcess As New Process
    4.     Dim StartInfo As New System.Diagnostics.ProcessStartInfo
    5.     StartInfo.FileName = "anexecutable" 'starts cmd window
    6.     StartInfo.Arguments = "somearguments"
    7.     StartInfo.RedirectStandardInput = True
    8.     StartInfo.RedirectStandardOutput = True
    9.     StartInfo.UseShellExecute = False 'required to redirect
    10.     StartInfo.CreateNoWindow = True 'creates no cmd window
    11.     cmdProcess.StartInfo = StartInfo
    12.     cmdProcess.Start()
    13.  
    14.     Dim SR As System.IO.StreamReader = cmdProcess.StandardOutput
    15.     Dim SW As System.IO.StreamWriter = cmdProcess.StandardInput
    16.     SW.WriteLine(OutputTextBox.Text) 'the command you wish to run.....
    17.     SW.WriteLine("exit") 'exits command prompt window
    18.     Results = SR.ReadToEnd 'returns results of the command window
    19.     SW.Close()
    20.     SR.Close()
    21.     'invokes Finished delegate, which updates textbox with the results text
    22.     Invoke(Finished)
    23.  
    24. End Sub

    By the way, (I don't know where to ask) how can I wrap my codes in this site with vb.net style?
    Last edited by nikel; May 21st, 2015 at 05:33 AM. Reason: I edited highlight

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with ReadToEnd and output freeze

    ReadToEnd won't return until there's an end to read to. If you want to read the output in blocks then call some other method, e.g. ReadLine or Read.
    Quote Originally Posted by nikel View Post
    By the way, (I don't know where to ask) how can I wrap my codes in this site with vb.net style?
    When you post code snippets inside highlight tags, you get to specify an option for formatting the highlighting. Not surprisingly, if you want VB.NET syntax highlighting then the option should be "vb.net", i.e.

    [HIGHLIGHT=vb.net]your code here[/HIGHLIGHT]
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Problem with ReadToEnd and output freeze

    Hello, thank you for your reply. This works fine:

    vb.net Code:
    1. Results = Results & SR.ReadLine & ControlChars.NewLine
    2. Results = Results & SR.ReadLine & ControlChars.NewLine

    But how can I learn how many lines in that result?

    EDIT: I noticed that using readline does the same thing. I also tried ReadLineAsync.
    Last edited by nikel; May 21st, 2015 at 07:01 AM. Reason: I edited the post

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Problem with ReadToEnd and output freeze

    Instead of doing it that way call Process.BeginOutputReadLine and then create an event handler for the OutputDataReceived event.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width