473,748 Members | 4,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Process StandardOutput and StandardError on seperate threads.

Hi all,

I have reposted this question from dotnet.general as I have been advised
that this is a more appropriate forum for this question. Apologies for the
repost.

I have a process thats starts in my application and only terminates when my
application is terminated. I want to write the output and the errors of this
process to a seperate log file. In order to do this, I spawned two threads.

My code looks something like this

' Starting the process
oProcessStartIn fo = New ProcessStartInf o()
With oProcessStartIn fo
.FileName = strFilename
.Arguments = strArguments
.UseShellExecut e = False
.CreateNoWindow = True
.RedirectStanda rdOutput = True
.RedirectStanda rdError = True
End With
oProcess = Process.Start(o ProcessStartInf o)
' Starting the threads
oOutputThread = New Thread(AddressO f ReadStdOut)
oErrorThread = New Thread(AddressO f ReadStdError)
With oOutputThread
.Name = "StandardOutput "
.Priority = ThreadPriority. BelowNormal
.Start()
End With

With oErrorThread
.Name = "StandardEr ror"
.Priority = ThreadPriority. BelowNormal
.Start()
End With

Private Sub ReadStdOut()
' Has to run in a seperate thread
' ReadtoEnd will work only if the process is getting terminated.
Dim str As String = oProcess.Standa rdOutput.ReadLi ne
Try
Do While str.Length >= 0
If str.Length <> 0 Then
Me.oLog.WriteLo g("Standard Output : " & str) ' Writes to
a log file.
End If
str = oProcess.Standa rdOutput.ReadLi ne
Loop
Catch
Return
End Try
End Sub

I have a similair ReadStdError function and both share the same instance of
the logging class.
When writing in the log class I implement ReadWriteLock

ReadWriteLock.A cquireWriterLoc k(System.Thread ing.Timeout.Inf inite)
Try
With oStreamWriter
.BaseStream.See k(0, SeekOrigin.End)
.WriteLine(sDat e & " : " & sMessage)
End With
Finally
ReadWriteLock.R eleaseWriterLoc k() ' Release the write lock.
End Try
I would like to know if this is the best approach. It works well for me.
However I felt that if I have an infinite loop while reading the standard
output stream, my application would be memory and process intensive and that
hasnt been the case. So I am a bit curious to know why it hasnt been
intensive.

Couple of additional points (not sure of its impact to my question)
- The process doesnt has a irregular stream of data coming from Standard
Output and its not very oft occuring.
- The StandardError by its very nature also doesnt have regular data coming
from it unless there is a real issue. I dont expect any errors if my setups
are correct.
- The Do While Loop should constantly be checking ReadLine - and thats my
real concern

Greateful for any thoughts you may have. I am a complete greenhorn in
threading.

Regards,
Wazir

Jul 21 '05 #1
2 2262
Please ignore this email. I posted to this group by mistake :(
Jul 21 '05 #2
Hi Mwazir,

This is still the dotnet.general

:-)

Cor
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10405
by: Al Cohen | last post by:
I'll start by warning that I'm a newbie to C# (but I've been programming for 25 years), so I may just be doing something reallyreally dumb. I'm writing a C# wrapper for a command-line application (pscp.exe, a secure file-copy app that's part of the excellent PuTTY SSH package). Getting pscp.exe to run properly was a piece of cake using the System.Diagnostics.Process class. The thing that I can't get to work is the ability to read...
5
3670
by: ask | last post by:
Hi NG I'm a bit new to programming c# and have a question regarding ftp by the command prompt. As far as I can see it should be possible to start a process and pipe command streams into it. But I cant make it work, it always stalls when I try to read the output: /// code 1 Process ftpProcess = new Process();
4
13159
by: Steve | last post by:
I am using Diagnostics.Process to, well.. execute a process. I would like to display the output of the process to my UI as it is created. For example, ping www.yahoo.com will slowly output each result. I want to display that output as it happens in my UI. I don't see a clean way to do this. After my Start() call, I made a call to StandardOutput.ReadToEnd() but it appears to hang until the process finishes (makes sense) But what do...
1
5986
by: solex | last post by:
Hello All, Hopefully someone has run into this error. I have written a class(source below) that launches a thread to monitor the StandardOutput of a System.Diagnostics.Process, in particular I am executing the find.exe program. The application works perfectly in the development environment. As soon as I execute the compiled program and start the find process I get an "Application Error". The error states that the instruction...
1
1965
by: mwazir | last post by:
Hi all, I have reposted this question from dotnet.general as I have been advised that this is a more appropriate forum for this question. Apologies for the repost. I have a process thats starts in my application and only terminates when my application is terminated. I want to write the output and the errors of this process to a seperate log file. In order to do this, I spawned two threads.
0
1495
by: BasicQ | last post by:
I am running an executable from my aspx page with the click of a button. A date is passed as an argument. I am able to get the standardoutput from the Process(Exe) into the label of my page after the process has completed executing. My problem is I need to get the output in the label simultaneously when the EXE is running, not after it has completed executing. Just like when I run the Exe in the cmd prompt. In my code I am opening two...
2
2279
by: mwazir | last post by:
Hi all, I have a process thats starts in my application and only terminates when my application is terminated. I want to write the output and the errors of this process to a seperate log file. In order to do this, I spawned two threads. My code looks something like this ' Starting the process oProcessStartInfo = New ProcessStartInfo()
11
11959
by: Jon Davis | last post by:
Does anyone know why using System.Diagnostics.Process to "wrap" a console application does not always transmit the I/O, depending on what processes you're trying to "consume"? PowerShell, for example, does not seem to process any I/O through the Process object. I know that in the case of PowerShell there are better ways to "wrap" the console by directly interfacing with the assemblies of System.Management.Automation or some similarly...
1
17317
by: stemp1ar | last post by:
I am wondering if it possible to open a single process and run multiple commands on that process and check standard error and standard out after each command? Has anyone done something similar and has an example? I am expecting it would look something like below... // Execute the first command proc.StartInfo.FileName = @"cmd.exe"; proc.StartInfo.Arguments = @"dir";
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9534
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.