|
I'm trying to read the StandardOutput of a process while it is running, but I'm unable to read anything from the StandardOutput before the process actually finishes. Here's what I currently have. I was trying to multithread it in the hope that that would solve the problem but it still doesn't, although it doesn't hang up my whole application anymore The method PutFiles is called first:
<br />
public void PutFiles(string FilePath)<br />
{<br />
string args = "-r -pw " + m_Password + " " + FilePath + " " + m_UserID + "@" + m_Address + ":" + m_CalcPath;<br />
m_ProcessArgs = args;<br />
m_ProcessApp = "pscp.exe";<br />
Thread myThread = new Thread(new ThreadStart(TransferProcess));<br />
myThread.Start();<br />
}<br />
<br />
private void TransferProcess()<br />
{<br />
ProcessStartInfo pInfo = new ProcessStartInfo(m_ProcessApp,m_ProcessArgs);<br />
pInfo.UseShellExecute = false;<br />
pInfo.RedirectStandardOutput = true;<br />
pInfo.RedirectStandardError = true;<br />
pInfo.CreateNoWindow = true;<br />
m_Running = Process.Start(pInfo);<br />
m_StdOut = m_Running.StandardOutput;<br />
Thread InfoThread = new Thread(new ThreadStart(ProcessInfo));<br />
InfoThread.Start();<br />
m_Running.WaitForExit();<br />
MessageBox.Show("Transfer Thread Done");<br />
}<br />
<br />
private void ProcessInfo()<br />
{<br />
MessageBox.Show(m_StdOut.Read().ToString());<br />
}<br />
The MessageBox in the ProcessInfo thread will not be displayed until the process is actually finished. If I don't try reading the stream in the ProcessInfo method then it'll display a textbox immediately. I've tried Read, ReadLine and ReadToEnd, but they all do the same thing. What I'm trying to do is grab the StandardOutput while it is being produced so that I can echo it to the user and they'd know whats going on.
Anybody got any ideas on this one?
|
|
|
|
|
You have to grab the stdout before you start the process. This is even in the docs. Since it's a reference, the reference will be non-null when the loader allocates resources for the new process.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
What exactly do you mean? I tried this:
<br />
ProcessStartInfo pInfo = new ProcessStartInfo(m_ProcessApp,m_ProcessArgs);<br />
pInfo.UseShellExecute = false;<br />
pInfo.RedirectStandardOutput = true;<br />
pInfo.RedirectStandardError = true;<br />
pInfo.CreateNoWindow = true;<br />
m_Running = new Process();<br />
m_Running.StartInfo = pInfo;<br />
m_StdOut = m_Running.StandardOutput;<br />
m_Running.Start();<br />
But I get an exception saying that StandardOutput has not been redirected. What docs are you looking at? The only ones that I've found only mention that you should read the StandardOutput before you wait for the process to finish because otherwise the pipe can fill up and hang both processes (the one you started and the parent). I haven't seen any suggesting that you should try and read it before starting the process!
|
|
|
|
|
The docs I'm talking about are the .NET Framework SDK docs, specifically for Process.StandardOutput ; however, when I looked here at my newest MSDN Library at home, you're right - you can't read it until after the call to Process.Start . The documentation I have at work is an older version - looks like it's time to update!
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Okay, thats what I was looking at too. Thanks for trying to help.
I've looked everywhere I can think of, but I can't find any examples of doing this. I'm wondering if it might be the case that the process that I'm running has to fill the pipe before anything can be read out of the StandardOutput? Maybe it's the case that my process just isn't writting enough for the pipe to be flushed into the StandardOutput stream? Therefore, I have to wait until the process is complete before anything gets written to the stream. The process that I'm running can take a while to run, but it doesn't write all that much to the console while it's running. Does anybody know if this is likely and if there is a way around it?
|
|
|
|
|
I have looked High and low to get this resolved, but still no luck .
I am trying to get some VB6 forms to show from within my C# windows app.
I have created a VB6 COM component (ActiveX dll) that has a method that is able to display the VB6 forms I need. This works fine in a VB6 test harness. but if I fire it off from a button in my C# windows form i get an ActiveX error:
Non-modal forms cannot be displayed in this host application from an ActiveX DLL, ActiveX Control, or Property Page
The only way the VB6 forms will show up in my C# app is if I have the forms in VB6 show as Modal, BUT I absolutely need to show them as modeless.
please help. I'm tearing whats left of my hair out!
|
|
|
|
|
I have a multilie textbox and I only want it to contain HEX characters. I can stop non-hex characters being typed in as I check what keys are being pressed, however I am having problems with pasting text into the textbox.
There are 2 ways to paste : via the context menu or pressing ctrl+v
I can detect a right click (which brings up the context menu) but I can't detect what menu item is selected from the context menu. I want to find out if paste has been selected and then validate the text and reject the paste if it contains non-hex characters.
I want to do the same for the keyboard shortcut for paste i.e. ctrl+v
Any help appreciated
Eco
|
|
|
|
|
Could you not hook the TextChanged event of your text box? I think that event should be fired when text is pasted into you text box regardless of how it was pasted. I'm not sure if you're going to be able to restore the old text if you decide to reject the new text though? Perhaps you could copy the text to a member varible in the TextChanged event (if the new text is valid) then if the new text is invalid you'd be able to restore it from the last time it was changed to something valid.
Yeah, I think that should work?
|
|
|
|
|
Hi,
I'm trying to catch an event fired by a radio button in a html page.
for this reason I created a console application in c#.
Until now, I managed to open an internet explorer instance,
and got a connection to the document via the mshtml.IHTMLDocument2 function.
After receiving the documentcomplete event my eventhandler mthlEvents.onmousedown
reacts if I click in the Internet Explorer with the mouse.
But my Radio Button is not marked and I see no way, to catogorize between different
Radiobuttons (i.e. No.1 and No.2).
I tried the example by Nikhil Dabas: Handling Document Events, too.
But the axshdocvw.dll is not available in a console application.
The shdocvw.dll which I'm using does not know the
DWebBrowserEvents2_DocumentCompleteEvent.
So I can't run this sample.
If somebody is more specialisted than me, please contact me.
Thanks Sonja
|
|
|
|
|
hello,
how I can make a shortcut for button in .net smart device application (or key accelerator)?
Ignatov
|
|
|
|
|
hi,
is it possible to run a method with parameter in new Thread like this?
public void WorkerThread(int a, String b)
{
// worker thread
}
public void Main()
{
Thread th = new ThreadStart(WorkerThread); // want to pass int a, String b to WorkerThread(), how can do that?
}
|
|
|
|
|
1. Pass the variables
2. Start the thread
public class WorkerClass{
int a;
String b;
public Thread GetThread(int a, String b){
this.a = a;
this.b = b;
return new Thread(new ThreadStart(ThreadMethod));
}
private void ThreadMethod(){
//do something
}
}
public void Main() {
Thread th = WorkerClass.GetThread();
}
|
|
|
|
|
How i can make that when i scroll one textbox (multiline=true) the second will be set at the same position like first???
Greetings
S_W
|
|
|
|
|
Why don't u do something like this: when a scroll event happens in the first text box, invoke another on the second text box?
|
|
|
|
|
Yea i know but how i can scroll another textbox??
Some idea?
S_W
|
|
|
|
|
I just created my first "Class" library and now I want to use it in my code. I added the Class Library by using the "Add Existing Item" from the Project menu and selected MyString.cs. When I try to build the project, I get an error message on line:
textBox2.Text = MyString.NumericOnly(textBox2.Text);
The build output says: d:\mcp2003w\WindowsApplication1\Form1.cs(179): The type or namespace name 'NumericOnly' does not exist in the class or namespace 'MyString' (are you missing an assembly reference?)
The code for the Class Library is:
using System;
using System.Text;
namespace MyString
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class MyString
{
public MyString()
{
//
// TODO: Add constructor logic here
//
}
public string NumericOnly(string input)
{
char[] textin;
int i;
StringBuilder textout;
textin = input.ToCharArray( );
textout = new StringBuilder( );
for (i=0; i<textin.Length; i++)
{
if (textin[i] >= '0' && textin[i] <= '9')
textout.Append(textin[i]);
}
return (textout(ToString());
}
}
}
Any idea what I did wrong??
Thanks,
Glenn
|
|
|
|
|
Well one solution is to change the namespace to match the namespace of your project. There might be a better one out there but that should do it.
|
|
|
|
|
That may do it, but I want to be able to use this library for multiple projects. I thought that was the idea behind object oriented programming. Building parts..
Glenn
|
|
|
|
|
gmhanna wrote:
I added the Class Library by using the "Add Existing Item" from the Project menu and selected MyString.cs.
If you want to add reference to your Class library use "Add Reference" from Project Menu. Then if your class MyString is in namespace MyNamespace you can use it in another project like this:
using MyNamespace;
MyString str=new Mystring();
string s=MyString.NumericOnly("1234a");
or
MyNamespace.MyString str=new MyNamespace.Mystring();
string s=MyNamespace.MyString.NumericOnly("1234a");
In your code you are not using MyString namespace so compiler thinks that MyString is namespace and not class as you wanted.
Hope this will help
i'm only pointer to myself
|
|
|
|
|
I need to know how to get a specific pixel's color on a panel, lable or image???
Leon v Wyk
|
|
|
|
|
Use the GDI+ GetPixel method
|
|
|
|
|
Hi...I'm trying to call an API, which takes a pointer to a structure that contains unions. I'm not sure how to build this in C#. Where I'm having difficulty is what to do for the unions and how setting a length for the character array. I know I'm going to have to use UNSAFE code for the dereferenced pointer.
I have enclosed the subset of the structure below...
typedef struct CCVUSER
{
SHORT TransactionType;
SHORT ProcessingMode;
PVOID pvBatch;
UCHAR uchErrorText1[255];
UCHAR uchErrorText2[255];
UCHAR uchSettingsPath[255];
union CCV
{
struct CreditCard
{
CHAR *CardInfo;
CHAR TransactionDate[4];
CHAR InvoiceNumber[10];
double CashBackAmount;
double TipAmount;
double Amount;
BYTE CustomerPresentFlag;
} CreditCard;
struct CheckApprovalRequest
{
CHAR IDType[2];
CHAR Number[26];
CHAR DateofBirth[6];
double Amount;
CHAR CheckSequenceNum[4];
} CheckApprovalRequest;
} CCV;
} CCVUSER;
Thanks for your help...
Glenn
Glenn
|
|
|
|
|
Here is a C style union in Csharp.
Out of the best Book I have read so far for .Net.
Essential .Net Volume 1.
[StructLayout( LayoutKind.Explicit )]
public struct Number
{
private enum NumType
{
Double,
Int64
}
[FieldOffset( 0 )]
double dblValue;
[FieldOffset( 0 )]
long lngValue;
[FieldOffset( 8 )]
NumType type;
public void Set( double value )
{
type = NumType.Double;
dblValue = value;
}
public void Set( long value )
{
type = NumType.Int64;
lngValue = value;
}
public double GetDouble()
{
if ( type == NumType.Double )
{
return dblValue;
}
return (double)lngValue;
}
public long GetLong()
{
if ( type == NumType.Int64 )
{
return lngValue;
}
return (long)dblValue;
}
}
Thank You
Bo Hunter
|
|
|
|
|
|