Question System.Windows.Forms.Application.DoEvent() inside a windows service?

Bill-e

Member
Joined
Jan 24, 2012
Messages
9
Programming Experience
3-5
Hi again, Second post of the day for me... sorry.

I have the following code that work fine in my windows test app:

Public Function GetAuditCode()
'////Get Audit Code +++++++++++++++++++++++


Dim p As New Process
p.StartInfo.UseShellExecute = False
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.StandardOutputEncoding = System.Text.Encoding.ASCII
p.StartInfo.FileName = My.Settings.AppDir
p.StartInfo.Arguments = "-audit"
p.Start()
Dim i As Integer = 0
Dim c(1) As Char
While Not p.StandardOutput.EndOfStream


p.StandardOutput.Read(c, 0, 1)
Me.TextBox1.Text += c(0).ToString()
strAudit = strAudit + c(0).ToString()
i += 1
Application.DoEvents()


End While
Return strAudit
End Function

The thing is I need this functionality in a Windows Service...

Is there something that can do this for me in a Windows Service?

TIA!
 
If the whole idea with the application is to interact with user you should think twice about using a windows service at all.
Starting a process should work as it is in a Windows Service application, if there was a window that would not be visible to the interactive user, but here there isn't anyway. Services don't have any user interface, so the part using controls (including DoEvents) is not relevant, the service can write output for example to a file.
You can have a regular forms application that can show things to user, whether that application should communicate with the service in any way is up to you.
 
Hi John,

I don't want any user interaction at all this is just the only way I could find to run a dos command I need to run.

I need to run the following "thingie.exe - audit" say this exe is in d:\MyApp

This out puts a string of text which I want to save in a text file.

The only way I could figure out to run this command and get the results back into the windows service was as above.

Is there othere ways?

I'm trying to call a bat file which does what I need to do via System.Diagnostics.Process.Start(cmd.exe, "d:\MyApp\thingie.exe - audit" >> Textfile.txt") But is can't find the file or just opens the cmd in the directory of the windows service...

I'm lost :I
 
This out puts a string of text which I want to save in a text file.
You can use IO.File.WriteAllText method to write the file, and p.StandardOutput.ReadToEnd to get all output from the process.
 
Thanks John,

I gave that a try also but it won't let me pass in parameters.

Is there any other ways in windows service i can call a .bat file and pass in a variable?
 
Figured it out! Just needed to sleep on it. I used the following to run the .bat and pass in parameters:
VB.NET:
Dim dosComm As String
        dosComm = Chr(34) & "D:\Program Files\Qas\Qas Batch Api\BATWV.EXE" & Chr(34) & " " & Chr(34) & "ParaPass.txt" & Chr(34)


        Dim go As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo("cmd.exe")
        go.UseShellExecute = False


        go.RedirectStandardInput = True


        go.RedirectStandardOutput = True


        go.RedirectStandardError = True


        go.WorkingDirectory = "D:\\Audit\\"




        Dim proc As System.Diagnostics.Process = System.Diagnostics.Process.Start(go)


        Dim sin As System.IO.StreamWriter = proc.StandardInput


        Dim sout As System.IO.StreamReader = proc.StandardOutput


        sin.WriteLine("PassParam.bat " & dosComm)


This runs the bat which in turn runs the exe and does what I need.


~Cheers!
 
Back
Top