Click here to Skip to main content
15,898,222 members
Home / Discussions / C#
   

C#

 
AnswerRe: c# controlbox and window menu Pin
Pete O'Hanlon24-Nov-10 3:32
mvePete O'Hanlon24-Nov-10 3:32 
QuestionHOWTO: start, get partial output and kill a console application from c# Pin
jancg23-Nov-10 22:06
jancg23-Nov-10 22:06 
AnswerRe: HOWTO: start, get partial output and kill a console application from c# Pin
Fla_Golfr23-Nov-10 22:38
professionalFla_Golfr23-Nov-10 22:38 
GeneralRe: HOWTO: start, get partial output and kill a console application from c# Pin
jancg23-Nov-10 23:05
jancg23-Nov-10 23:05 
AnswerRe: HOWTO: start, get partial output and kill a console application from c# Pin
Luc Pattyn24-Nov-10 3:36
sitebuilderLuc Pattyn24-Nov-10 3:36 
GeneralRe: HOWTO: start, get partial output and kill a console application from c# Pin
jancg24-Nov-10 4:22
jancg24-Nov-10 4:22 
GeneralRe: HOWTO: start, get partial output and kill a console application from c# Pin
jancg24-Nov-10 10:06
jancg24-Nov-10 10:06 
AnswerRe: HOWTO: start, get partial output and kill a console application from c# Pin
Luc Pattyn24-Nov-10 10:43
sitebuilderLuc Pattyn24-Nov-10 10:43 
So you're saying that attempt failed. There are maybe a dozen more ways to do things. I suggest you dedicate a thread (could be a BGW); that way you don't need a timer, don't need Peek at all, you just do a blocking ReadLine in a loop, until the main thread decides all is done. I did this, all the way back, using .NET 1.0 and Windows 98, and I can assure you there have been some bugs in .NET 1.x making it pretty hard; Since NT/XP the code got simpler and it now runs like a charm, one thread for stdout, one for stderr, and their output intertwines as it would in a DOS window (Command Prompt).

Here is a typical receiver thread (the output() method is likely to contain a Control.Invoke):
private void stdoutReader(object dummy) {
    for (; threadsRunning && stdoutLines<maxLines; stdoutLines++) {
        string s=stdout.ReadLine();
        if (s==null) break;
        output(s);
    }
    stdoutDone=true;
}


Mind you, there is some buffering going on, in the target app and in your app, so don't expect the output right away, there will be say some delays, but not seconds, and certainly not till the target app finishes. And the DOS prompt/command window (or something deep inside Windows) is smart enough to adapt the buffering depending on how things get used, so a pipe may well have a larger buffer than a direct interactive invocation.

Due to the buffering and sometimes automatic but delayed flushing, determining the end of the output may be a little tricky too; here is one possibility (code executed in the main thread):
proc.WaitForExit();
// wait for threads to consume all output
for (; ; ) {
    if (stdoutDone&&stderrDone) break;
    int cur=stdoutLines+stderrLines;
    Thread.Sleep(500);
    if (cur>=stdoutLines+stderrLines) break;
}
threadsRunning=false;


Smile | :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.


modified on Wednesday, November 24, 2010 5:09 PM

GeneralRe: HOWTO: start, get partial output and kill a console application from c# Pin
jancg24-Nov-10 12:07
jancg24-Nov-10 12:07 
GeneralRe: HOWTO: start, get partial output and kill a console application from c# Pin
Luc Pattyn24-Nov-10 12:29
sitebuilderLuc Pattyn24-Nov-10 12:29 
AnswerRe: HOWTO: start, get partial output and kill a console application from c# Pin
PIEBALDconsult24-Nov-10 11:59
mvePIEBALDconsult24-Nov-10 11:59 
QuestionMessage Removed Pin
23-Nov-10 20:19
NarVish23-Nov-10 20:19 
AnswerRe: Logging the start & end of the function of an application. Pin
JF201523-Nov-10 20:24
JF201523-Nov-10 20:24 
QuestionNeed Typing Tutor Method / Examples Pin
M Riaz Bashir23-Nov-10 18:46
M Riaz Bashir23-Nov-10 18:46 
AnswerRe: Need Typing Tutor Method / Examples Pin
Keith Barrow23-Nov-10 21:49
professionalKeith Barrow23-Nov-10 21:49 
QuestionGet color value from array Pin
pancakeleh23-Nov-10 16:49
pancakeleh23-Nov-10 16:49 
AnswerRe: Get color value from array Pin
JF201523-Nov-10 18:25
JF201523-Nov-10 18:25 
GeneralRe: Get color value from array Pin
pancakeleh23-Nov-10 18:31
pancakeleh23-Nov-10 18:31 
GeneralRe: Get color value from array Pin
JF201523-Nov-10 18:37
JF201523-Nov-10 18:37 
GeneralRe: Get color value from array [modified] Pin
pancakeleh23-Nov-10 18:52
pancakeleh23-Nov-10 18:52 
GeneralRe: Get color value from array Pin
_Erik_24-Nov-10 3:48
_Erik_24-Nov-10 3:48 
GeneralRe: Get color value from array [modified] Pin
pancakeleh24-Nov-10 16:11
pancakeleh24-Nov-10 16:11 
GeneralRe: Get color value from array [modified] Pin
_Erik_25-Nov-10 4:51
_Erik_25-Nov-10 4:51 
QuestionWhat Class(es) to use for Hardware Profiling? Pin
dpminusa23-Nov-10 15:48
dpminusa23-Nov-10 15:48 
AnswerRe: What Class(es) to use for Hardware Profiling? Pin
Not Active23-Nov-10 15:58
mentorNot Active23-Nov-10 15:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.