Results 1 to 11 of 11

Thread: [RESOLVED] Console.UpdateText?

  1. #1

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Resolved [RESOLVED] Console.UpdateText?

    I am sure some of you know of Handbrake, and it's CLI. Now I am trying use this CLI, and read its standard output to get the progress. But the problem is, they are not using Console.WriteLine to write the progress in their CLI, they are using something else

    If I read the standardoutput I only get this line:

    Encoding task 1 of 2 blabla 0.00%

    But if I don't redirect outputs or set CreateNoWindow to true, so the CLI shows up in its own window, I can see that the progress is ticking.

    What I wonder is if there exist anything like Console.UpdateText, where you don't write a newline, you just edit the last written one.
    And if so, how to read it?

    I am reading with this code:

    Code:
    string str;
    
                while ((str = prc.StandardOutput.ReadLine()) != null)
                {
                        Console.Write(str);
                }
    Last edited by Cyb3rH4Xter; Feb 5th, 2011 at 11:14 AM.

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

    Re: Console.UpdateText?

    You are calling ReadLine, which reads text up to the next line break. ReadLine doesn't return until a line break is entered. Presumably whatever they are outputting doesn't contain any line breaks. Try calling one of the other reading methods that doesn't rely on a line break.
    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
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Console.UpdateText?

    Quote Originally Posted by jmcilhinney View Post
    You are calling ReadLine, which reads text up to the next line break. ReadLine doesn't return until a line break is entered. Presumably whatever they are outputting doesn't contain any line breaks. Try calling one of the other reading methods that doesn't rely on a line break.
    thx for answer,

    I tried the ReadToEnd, though it only reads when I close the other process.
    I tried using the Read and ReadBlock though I don't understand how they work?

    EDIT: I took a try on this:http://www.codeproject.com/KB/thread...chprocess.aspx
    and it can read okay, so I will use that method instead (adding handlers for stdoutput received).
    Last edited by Cyb3rH4Xter; Feb 5th, 2011 at 01:29 PM.

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Console.UpdateText?

    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Console.UpdateText?

    Quote Originally Posted by Pradeep1210 View Post
    Great blog!
    I will try to use that method instead, more simple. But I got one problem, I don't know to add handlers for the DataReceived in C# :S

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

    Re: Console.UpdateText?

    Quote Originally Posted by Cyb3rH4Xter View Post
    But I got one problem, I don't know to add handlers for the DataReceived in C#
    If you add the Process object to the form in the designer then you can use the Properties window to add the event handlers, just like any other control or component. If you want to add them in code then you do it like this:
    csharp Code:
    1. someObject.SomeEvent += new EventHandler(SomeMethod);
    If you start typing a line like that, once you type the = then VS will help you from there.
    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

  7. #7

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Console.UpdateText?

    Quote Originally Posted by jmcilhinney View Post
    If you add the Process object to the form in the designer then you can use the Properties window to add the event handlers, just like any other control or component. If you want to add them in code then you do it like this:
    csharp Code:
    1. someObject.SomeEvent += new EventHandler(SomeMethod);
    If you start typing a line like that, once you type the = then VS will help you from there.
    Thx, halfway through now

    It won't accept this:

    Code:
    prc.OutputDataReceived += new DataReceivedEventHandler(OutputDataReceived);
    Code:
            private void OutputDataReceived(Object sender, System.Diagnostics.DataReceivedEventArgs e)
            {
    
                Console.Write(e.Data);
            }
    Error:

    Code:
    An object reference is required for the non-static field, method, or property 'QuickBrake.Program.OutputDataReceived(object, System.Diagnostics.DataReceivedEventArgs)'
    EDIT: Solved by changing to private static void thx for help

    EDIT2: I am just curious, can I do like them and just kind of update the text, not write a new line or anything like that?

    LASTEDIT: Solved that too, adding \r to the beginning of the string moves the cursor back and makes it overwrite last (Console.Write("\r Hello")
    Last edited by Cyb3rH4Xter; Feb 6th, 2011 at 04:49 AM.

  8. #8
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: [RESOLVED] Console.UpdateText?

    Whew this harkens back to my old DOS programming days.

    In the DOS console (not the CMD Shell - they're different) one can send BIOS Interop codes to move the cursor around the shell. You can not only rewrite the line you're on - I used to write information at the top line and bottom line of the screen with every update and then put the cursor back to where it started.

    Those were the days! (I wrote in Fortran then!!)

    The escape characters you would use for that are listed here, but are a bit cryptic to puzzle out:
    http://local.wasp.uwa.edu.au/~pbourk...formats/ascii/

    (Scroll down to "ANSI Standard (X3.64) Control Sequences For Video Terminals And Peripherals In Alphabetic Order By Mnemonic")

    The escape sequence to set a position would be:

    Code:
    //Code not tested
    //Let's set the position to 3, 12
    byte HorizontalOffset = (byte) 3;
    byte VerticalOffset = (byte) 12;
    char[] MovePosition = {27, '[', HorizontalOffset, ';', VerticalOffset, 'H'};
    Console.Write(MovePosition);
    Though I can't test this right now (about to be away from the computer). And it may not work in the console at all anymore.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  9. #9
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: [RESOLVED] Console.UpdateText?

    Haha. They saw me coming. The code above doesn't work (it just displays garbage which is what I expected)

    ... but the following does work:

    Code:
    			Console.SetCursorPosition(3, 12);
    			Console.Write("Test");
    			Console.ReadKey();
    Talk about elephant gun and fly...
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  10. #10
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: [RESOLVED] Console.UpdateText?

    To extend the example, this writes " Well?" then backs up, overwrites the space with an "Sw" (Making it "Swell?") and returns the input back to where it was (after the question mark).

    Code:
    			Console.WriteLine("Test Line");
    			Console.Write(" Well?");
    			int Horizontal = Console.CursorLeft;
    			int Vertical = Console.CursorTop;
    
    			//Upper Left is 0, 0
    			Console.SetCursorPosition(0, Vertical);
    			Console.Write("Sw");
    			Console.SetCursorPosition(Horizontal, Vertical);
    			Console.ReadKey();
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  11. #11

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: [RESOLVED] Console.UpdateText?

    I see, great code

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