|
Thanks, but I don`t want to use any files.
Here is the code that i use, but it is not a real - time!
private void button2_Click(object sender, RoutedEventArgs e)
{
Thread Runst = new Thread(Run);
Runst.Start();
}
public void Run()
{
ProcessStartInfo processStartInfo = new ProcessStartInfo("C:\\test.exe");
processStartInfo.ErrorDialog = false;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();
while (!process.StandardOutput.EndOfStream)
{
textBox2.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate()
{
textBox2.Text += process.StandardOutput.ReadLine() + Environment.NewLine;
});
}
}
test.cpp
#include <stdio.h>
#include <windows.h>
int main()
{
for (int i=0;i<=30;i++)
{
fprintf(stdout,"%d) This is NORMALL stout (0-30)\n",i);
Sleep(100);
}
}
Also,this is not a solution!
process.OutputDataReceived += new DataReceivedEventHandler(CatchOutHandler);
|
|
|
|
|
I'm not sure, but here is my best guess:
it is all about the definition of "new line".
On Windows systems, a native C/C++ program creates a "new line" by outputting a \n which basically is the ASCII linefeed; Windows however by default wants a carriagereturn+linefeed, therefore most run-times will expand \n to \n\r automatically.
Again on Windows systems, a managed code program uses \n\r as newline string, see Environment.NewLine (Note: Mono on Unix would simply use \n here).
For serial ports, the user can set a different newline string, see SerialPort.NewLine
For streams, like you are using, there is no such facility.
Now my guess is that your C program, writing to a stream rather than a real output device (such as the console), doesn't get itsn expanded to \n\r, which your managed code is looking for; therefore the output is not multi-line, and only gets captured when the producer terminates.
If all the above applies, then these are some solutions:
1.
replace every \n by \n\r in your producer (C code)
2.
or stop using all the line-oriented stuff (the DataReceived handlers and the ReadLine method) in your consumer; instead use a thread with a loop, and read arbitrary strings. Note: two threads would be required if you wanted to deal with real-time stdout and stderr.
FWIW: the situation may get more complicated when you are on a virtual system; I have been told Citrix for example does not handle interprocess streams properly (although it might have been a \n versus \n\r issue there too).
|
|
|
|
|
Luc Pattyn, thank you very much!
That was very useful!
But, unfortunately, replacing \n by \n\r didn`t do the trick!
Besides, with a real-world app, I won`t have an opportunity to do that!
Because, the app which out I want to redirect, was written not by me,
and I don`t have its source code!
Please, can you provide me with a source code example,
which will use a thread with a loop, and reading arbitrary strings?
I didn`t understand this solution!
Thanks!
|
|
|
|
|
This is a first draft, I did not test it:
bool stdReading;
StreamReader stdout;
StreamReader stderr;
public void LaunchProcess() {
ProcessStartInfo psi=new ProcessStartInfo();
psi....
log("Starting process");
Process p=Process.Start(psi);
stdout=p.StandardOutput;
stderr=p.StandardError;
stdReading=true;
Thread outThread=new Thread(new ThreadStart(outReader));
outThread.Start();
Thread errThread=new Thread(new ThreadStart(errReader));
errThread.Start();
p.WaitForExit();
log("Process is done");
Thread.Sleep(300);
stdReading=false;
errThread.Join(300);
outThread.Join(300);
log("Reading streams in done");
}
public void outReader() {
int DIM=1024;
char[] buf=new char[DIM];
while (stdReading) {
int count=stdout.Read(buf, 0, DIM);
string s=new string(buf, 0, count);
...
}
}
public void errReader() {
int DIM=1024;
char[] buf=new char[DIM];
while (stdReading) {
int count=stdout.Read(buf, 0, DIM);
string s=new string(buf, 0, count);
...
}
}
Notes:
1. you may want to decrease DIM to get the output sooner when lines are short;
2. you may want to set Thread.IsBackground true so the threads don't prevent app termination.
|
|
|
|
|
Now I understood!
It Seems to work!!!
This is the magic solution!!!!
Luc Pattyn, thanks!
Thank you very much for all your help!!!
|
|
|
|
|
you're welcome.
|
|
|
|
|
|
Figured out, that if a native win32 console application
didn`t fflush its stdout and stderr,
redirecting its output is a real problem!
Is it possible to redirect 'bad' console apps,
(without fflush(stdout))?
Also found solution, by _Olivier_, but I don`t want to use
any stubs an 'between stuff'!
Real-Time Console Output Redirection[^]
Thank you all, for your help!
|
|
|
|
|
public class MyTextBox : TextBox
{
private static Dictionary<IntPtr, WeakReference> mList = new Dictionary<IntPtr, WeakReference>();
public MyTextBox()
{
mList.Add(Handle, new WeakReference(this));
}
}
Any problem for MyTextBox being garbage collected?
|
|
|
|
|
I recommend that you google ".net weak references". You'll get over 6.8 million results back, and there appear to be a number of good links on the first page of results.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
If nothing has a strong reference to MyTextBox, then yeah, it could get GC'ed. Thats why its called a WeakReference. Why would you use a WeakReference here anyways?
|
|
|
|
|
Making it weak reference will not prevent GC to play it's role. It's static. If it's not weak reference strong references of hundreds of textboxes in my app get polluted. I just bother with the textboxes that are really alive.
|
|
|
|
|
If you're relying on the reference being there, then you either need something that can recreate the reference when needed, or you need to maintain a strong reference.
|
|
|
|
|
I just bother with the textboxes that are really alive.
|
|
|
|
|
Objects of type MyTextBox will be collected if there are no strong references to them and, of course, the GC performs a garbage collection. mList dictionary will not be collected becouse there is a static strong reference to it, unless you clear this reference. I guess you have a static method or property to access the contents of mList Dictionary, becouse if you don't this dictionary is completely useless, and I don't see how you should need it unless MyTextBox class was much more heavy than it seems to be.
|
|
|
|
|
thomus07 wrote: private static Dictionary<intptr, weakreference="">
What do you think is going to happen with that IntPtr when the GC collects the TextBox?
|
|
|
|
|
How to extract any object from the image after convert it's to black and white.
|
|
|
|
|
If the image is of a top hat I might be able to extract a rabbit from it, otherwise it beyond my abilities.
|
|
|
|
|
Boom tish!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
a
Ramkumar
("When you build bridges you can keep crossing them. ")
http://ramkumarishere.blogspot.com
|
|
|
|
|
Ramkumar_S wrote: a
aa
|
|
|
|
|
|
Were you proposing to post a reply, with each letter in a separate message?
If so, this could take a while...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
|
|
|
|
|
So you think being abrupt and rude will get you an answer? Good luck with that.
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.
nils illegitimus carborundum
me, me, me
|
|
|
|