473,763 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Process StandardOutput in a seperate thread

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
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. Greateful for any thoughts you may have.

Regards,
Wazir
Nov 22 '05 #1
2 2281
Hi Mwazir,

Did you know that there is the newsgroup
microsoft.publi c.dotnet.langua ges.vb

This is a nice question for that newsgroup in my opinion.

You says it works so it is a nice piece of work.
What it does when I see it right is

Start a commandline proces.
Reads assynchroon the input for that process
Write assynchroon the output for that process.

I do not know if assynchroon use will have much benefit in this situation.

Assynchron have benefits when there are wait times in the proces, which will
spent time when they cannot be cached, otherwise they only use more
resources and time.
In this situation I do not see those wait times.

However maybe I see it wrong. As I said a good place to ask is the newsgroup
I mentionned above. However there are here also regulars who can answer that
in a more general way.

Cor


"mwazir" <mw*********@ho tmail.com> schreef in bericht
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
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
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. Greateful for any thoughts you may have.

Regards,
Wazir

Nov 22 '05 #2
Cor,

I will post this again to the microsoft.publi c.dotnet.langua ges.vb group, as
advised

Many thanks for your inputs,
Wazir
Nov 22 '05 #3

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

Similar topics

2
2263
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.
3
10407
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...
2
1705
by: MattMenard | last post by:
I've written a C program that I want to run inside an Gui wrapper that I started to write in MFC. I have to select some files then kick off a process running the C program. I have the initial dialog file selection stuff written, but am not sure how to run the C program inside a seperate thread. I'm using VS.NET 2003 and am trying to use the Thread and Process objects, but am not sure if I can use that with MFC. I can't compile the code...
1
1686
by: Scott | last post by:
I am trying to write an asp.net web page that will spawn a third party .exe file and return the results back to my asp page. I am able to write a vb.net program that will do this just fine. I redirect the output to a msgbox and display it on the screen. However, when I put this as an asp.net web page, the process just hangs. I am able to do other processes from the same web page and get the results, but when I try to launch this one...
1
5989
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...
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
8634
by: Al | last post by:
I'm currently attempting to use PLink (the console component of PUTTY - see http://www.chiark.greenend.org.uk/~sgtatham/putty/) as a Telnet component as I may in future need to change to using SSH and this seems an ideal solution. I'm running it as a process and re-directing the standardinput/output/error However, despite working through all the different variations of code I can either think of or find I am unable to achieve true...
0
2245
by: smimon | last post by:
Hi I'm trying to run a DTS package from a ASP.NET web page using System.Diagnostics.Process. This DTS takes up to 10 minutes to complete, during which, output is generated which i would like to trap and output to a client. Having read a bit of literature about this, i realise that it is necessary to start a new thread to report on the progress of the process using StandardOutput. However, the first attempt to read from this stream causes...
5
3457
by: =?Utf-8?B?Z215ZXJz?= | last post by:
Hello, I am attempting to start a cmd.exe process and pass several .vbs scripts (with additional parameters) and then read the output from the scripts and make "notes" in a DataTable (the "notes" not being the issue). Beginning with... Dim objProcess As Process Dim objProcessStartInfo As New ProcessStartInfo
0
9563
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
9998
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...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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
7366
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
6642
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();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
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
3
3523
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.