I currently have a big problem, the code below checks for the keyword "mp4:production/CATCHUP/" in a file, and if found it will launch an executable, although because it finds multiple (exactly the same) instances of "mp4:production/CATCHUP/" it launches several processes. Is there anyway of restricting this, so that it may stop looking when found one instance?
My code is as follows:
string s = "";
private void CheckLog()
{
bool _found;
while (true)
{
_found = false;
Thread.Sleep(5000);
if (!System.IO.File.Exists("Command.bat")) continue;
using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat"))
{
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("test"))
{
_found = true;
break;
}
}
}
if (_found)
{
// Deletes filename in the log file, as the filename is instead handled by p.start
var result = Regex.Replace(s, #"test", string.Empty);
s = result;
RemoveEXELog(); // Deletes a specific keyword from Command.bat
RemoveHostFile();
Process p = new Process();
p.StartInfo.WorkingDirectory = "dump";
p.StartInfo.FileName = "test.exe";
p.StartInfo.Arguments = s;
p.Start();
p.WaitForExit();
MessageBox.Show("Operation Successful!");
string myPath = #"dump";
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = myPath;
prc.Start();
ClearLog(); // Deletes Command.bat and then creates a new empty Command.bat
LogTrue();
}
}
}
For this scenario I would use a Singleton class to manage the workflow. The Singleton would manage the equivalent to your _found variable in a global threadsafe manner. All threads would then query this property.
Something like the following:
public sealed class Singleton
{
private static volatile Singleton instance;
private static object syncRoot = new Object();
private Singleton() {}
public bool Found { get; set; }
public static Singleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}
Then your code would be something like the following:
private void CheckLog()
{
//bool _found; //not needed anymore
while (!Singleton.Instance.Found)
{
//_found = false;
Thread.Sleep(5000);
if (!System.IO.File.Exists("Command.bat")) continue;
using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat"))
{
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("mp4:production/CATCHUP/"))
{
Singleton.Instance.Found = true;
break;
}
}
}
if (Singleton.Instance.Found)
{
// Deletes filename in the log file, as the filename is instead handled by p.start
var result = Regex.Replace(s, #"rtmpdump", string.Empty);
s = result;
RemoveEXELog(); // Deletes a specific keyword from Command.bat
RemoveHostFile();
Process p = new Process();
p.StartInfo.WorkingDirectory = "dump";
p.StartInfo.FileName = "test.exe";
p.StartInfo.Arguments = s;
p.Start();
p.WaitForExit();
MessageBox.Show("Operation Successful!");
string myPath = #"dump";
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = myPath;
prc.Start();
ClearLog(); // Deletes Command.bat and then creates a new empty Command.bat
LogTrue();
}
}
}
As suggested by Hans Passant, what is wrong with the obvious stop looping once you'vre found it approach? No singletons required.
private void CheckLog()
{
bool found = false;
while (!found)
{
//your code ...
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("test"))
{
_found = true;
break;
}
}
if (found)
{
//some more of your code ...
}
else
{
//get ready for the next iteration
}
}
}
Related
I wrote program that listens on a directory (GUI - WPF). When the new file show up in this directory is sent to the printer. The problem occurs when I try to save a large file to this directory. I have to wait until the file is closed, and then send it to the printer. I have a function that checks if the file is open. But when I use it in the whole GUI hangs. How do I use this function asynchronously?
protected void newfile(object fscreated, FileSystemEventArgs Eventocc)
{
try
{
string CreatedFileName = Eventocc.Name;
FileInfo createdFile = new FileInfo(CreatedFileName);
string extension = createdFile.Extension;
string eventoccured = Eventocc.ChangeType.ToString();
fsLastRaised = DateTime.Now;
this.Dispatcher.Invoke((Action)(() =>
{
String file = "";
file = watchingFolder + "\\" + CreatedFileName;
//printing
this.Dispatcher.Invoke((Action)(() =>
{
FileInfo info = new FileInfo(file);
while (!IsFileReady(info)) { }
var t = new Thread(() => printFile(file, extension)); //send to printer
t.Start();
}));
}));
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Error");
}
}
IsFileReady function:
public static bool IsFileReady(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
And printfile
public void printFile(string filepath, string ext)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = filepath;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
}
How can I correct this code to work with large files without hangs up?
EDIT:
For check new file I use FileSystemWatcher
private void start(object sender, RoutedEventArgs e)
{
if (watchingFolder == null)
{
}
else
{
fs = new FileSystemWatcher(watchingFolder, "*.*");
fs.EnableRaisingEvents = true;
fs.IncludeSubdirectories = true;
fs.Created += new FileSystemEventHandler(newfile);
btnSatrt.IsEnabled = false;
btnStop.IsEnabled = true;
}
}
You're executing while (!IsFileReady(info)) { } through Dispatcher.Invoke, that executes the code on the UI thread so it will block for sure the app.
You aren't interacting at all with the UI, so the correct approach is to execute it asynchronously, via Tasks and awaits or with a background thread via the ThreadPool and not using at all Dispatcher.Invoke.
Try to execute all code in the newfile event handler on a background thread by starting a new task:
protected async void newfile(object fscreated, FileSystemEventArgs Eventocc)
{
try
{
await Task.Run(() =>
{
string CreatedFileName = Eventocc.Name;
FileInfo createdFile = new FileInfo(CreatedFileName);
string extension = createdFile.Extension;
string eventoccured = Eventocc.ChangeType.ToString();
fsLastRaised = DateTime.Now;
string file = watchingFolder + "\\" + CreatedFileName;
FileInfo info = new FileInfo(file);
while (!IsFileReady(info)) { }
printFile(file, extension);
});
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Error");
}
}
Use a BackgroundWorker instead of Dispatcher.Invoke.
i am trying the below code
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = new StreamWriter(p.StandardInput.BaseStream))
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(#"dir");
}
}
using (StreamWriter sw = new StreamWriter(p.StandardInput.BaseStream))
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(#"dir");
}
}
the first sw.writeline is going ok, but the second i discovered that the process hasexeited is true.
why the process exited , and how to keep it running to execute further commands?
Put both write commands in the same using block and omit the second using block. The using-block disposes the StreamWriter after you leave the block. Disposing the StreamWriter closes the underlying stream and causes cmd to exit.
i solved the problem like that
static Queue<string> CmdQueue = new Queue<string>();
static Thread ThreadCMD = new Thread(new ThreadStart(CMDThread));
private static void CMDThread()
{
bool Active = true; // used to indicate the status of the session
ProcessStartInfo psiOpt = new ProcessStartInfo();
psiOpt.FileName = "cmd.exe";
psiOpt.WindowStyle = ProcessWindowStyle.Hidden;
psiOpt.RedirectStandardOutput = true;
psiOpt.RedirectStandardInput = true;
psiOpt.RedirectStandardError = true;
psiOpt.UseShellExecute = false;
psiOpt.CreateNoWindow = true;
Process procCommand = Process.Start(psiOpt);
procCommand.OutputDataReceived += ProcCommand_OutputDataReceived;
procCommand.ErrorDataReceived += ProcCommand_ErrorDataReceived;
procCommand.BeginOutputReadLine();
procCommand.BeginErrorReadLine();
using (StreamWriter sw = procCommand.StandardInput)
{
while (Active)
{
while (CmdQueue.Count==0) { }
string cmd = CmdQueue.Dequeue();
if (cmd != "cmdexit")
{
sw.WriteLine(cmd);
}
else
{
Active = false;
}
}
}
procCommand.OutputDataReceived -= ProcCommand_OutputDataReceived;
procCommand.ErrorDataReceived -= ProcCommand_ErrorDataReceived;
procCommand.Close();
}
private static void ProcCommand_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
PushCMDResult(Encoding.ASCII.GetBytes(e.Data));
}
private static void ProcCommand_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
PushCMDResult(Encoding.ASCII.GetBytes(e.Data));
}
public void StartCMD()
{
ThreadCMD.Start();
}
public void Execute(string cmd)
{
CmdQueue.Enqueue(cmd);
}
i can start cmd.exe session , and execute as many as needed of commands using execute to enqueue the cmd string in Queue which will be written to the streamwriter .
i used async reading for output and error as well.
I want to perform some command lines to display the result after each input.
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("ftp");
//output
sw.WriteLine("open ftp.server.com");
//output
sw.WriteLine("username");
//output
sw.WriteLine("password");
//output
}
}
Help me to understand how to make the output result after each sw.WriteLine(...)?
Updated
It is not working with ftp. Why?
Initialization:
Test test = new Test();
test.start();
Console.ReadKey();
Class Test:
class Test
{
static StringBuilder StdOutput = new StringBuilder();
Process p = null;
Queue<string> cmdQueue = new Queue<string>();
public void start(){
cmdQueue = new Queue<string>();
cmdQueue.Enqueue("cd c:\\");
cmdQueue.Enqueue("dir");
cmdQueue.Enqueue("ftp");
cmdQueue.Enqueue("open us1.hostedftp.com");
cmdQueue.Enqueue("z3r9#ya.ru");
cmdQueue.Enqueue("123456");
cmdQueue.Enqueue("dir");
setupProcess();
startProcess();
}
private void setupProcess()
{
p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd";
info.CreateNoWindow = true;
info.RedirectStandardOutput = true;
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.OutputDataReceived += new DataReceivedEventHandler(OutputDataHandler);
StdOutput = new StringBuilder();
p.StartInfo = info;
}
private async void startProcess()
{
p.Start();
p.BeginOutputReadLine();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
while (cmdQueue.Count > 0)
{
string cmd = cmdQueue.Dequeue();
if (cmd != null & cmd != "")
{
await sw.WriteLineAsync(cmd);
Thread.Sleep(100);
//System.Console.WriteLine(StdOutput);
}
else
{
break;
}
}
Console.WriteLine(StdOutput);
}
p.WaitForExit();
}
}
private static void OutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
StdOutput.Append(Environment.NewLine + outLine.Data);
//System.Console.WriteLine(Environment.NewLine + outLine.Data);
}
}
}
I assume that you are actually asking about how to catch outputs from all the commands you want to have executed in the (one) process.
Here is a version of a solution I came up with a long time ago, when I was a rookie here..
The trick is to collect the output as is comes along by listening to events the Process will trigger whenever output gets created: OutputDataReceived and ErrorDataReceived. We need to run things async for this to work, so it will look a little more complicated than the usual examples, which only have one process executing one command..:
First a few variables:
Queue<string> cmdQueue = new Queue<string>();
static StringBuilder StdOutput = new StringBuilder();
static StringBuilder ErrOutput = new StringBuilder();
Process p = null;
Task processTask = null;
bool processIsRunning = false;
Here is a button click event that starts processing all commands from a multiline TextBox. Output gets collected in the two StringBuilders; when the queue is empty, I wait a little longer..:
private void button1_Click(object sender, EventArgs e)
{
cmdQueue = new Queue<string>(tb_commands.Lines.ToList());
setupProcess();
startProcessTask();
while (cmdQueue.Count > 0) Thread.Sleep(100);
Thread.Sleep(500);
tb_out.AppendText(StdOutput + "\r\n" + ErrOutput + "\r\n");
}
Here is the routine that set up the Process. Here we register two events that will notify us when there are lines in the output streams..:
private void setupProcess()
{
p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.CreateNoWindow = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.OutputDataReceived += new DataReceivedEventHandler(OutputDataHandler);
p.ErrorDataReceived += new DataReceivedEventHandler(ErrorDataHandler);
StdOutput = new StringBuilder();
ErrOutput = new StringBuilder();
p.StartInfo = info;
}
After the setup we can start a Task that will start our Process asynchonously..:
private void startProcessTask()
{
var task = Task.Factory.StartNew(() => startProcess());
processTask = task;
}
..and finally here is the async method that after starting the Process and beginning with the asynchronous read operations on the redirected streams, keeps feeding it all lines from the command queue.
private async void startProcess()
{
try { p.Start(); processIsRunning = true; } catch
{
ErrOutput.Append("\r\nError starting cmd process.");
processIsRunning = false;
}
p.BeginOutputReadLine();
p.BeginErrorReadLine();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
do
{
try
{
string cmd = cmdQueue.Dequeue();
if (cmd != null & cmd != "") await sw.WriteLineAsync(cmd);
} catch { }
} while (processIsRunning);
try { p.WaitForExit(); } catch { ErrOutput.Append("WaitForExit Error.\r\n"); }
}
}
The last pieces are the two events we have registered for reading the output from the two streams and adding them to the StringBuilders:
private static void OutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
StdOutput.Append(Environment.NewLine + outLine.Data);
}
}
private static void ErrorDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
ErrOutput.Append(Environment.NewLine + outLine.Data);
}
}
Note that this works fine for all sorts of commands you can feed into the process, including FTP. Here I change my codepage, show the images I have before, log in to an FTP server, call up the help page, cd and dir, download an image, close the connection and check the images I have now..:
One Caveat: There must be something wrong in the way I wrote this, as VS keeps complaining about a System.InvalidOperationException and the exe file hogs ~10% cpu. Helping me out would be very much appreciated..
I'm making a simple program in C# that allows a client to connect to a server and run MS DOS commands on that server.
The program worked well, later on I decided I wanted to cmd.exe process to run whilst the client was connected to the server, this is where I'm stuck at. I wanted this so that I could
run commands like CD and change the working directory, previously this would've had no effect because the cmd.exe process was closed after the command was run.
It appears that I can only read from the StandardOutput and StandardError streams if the process has exited, is there any workaround for this?
Here's some of my code used in the program :
*Creates and returns cmd.exe process: *
private Process createDOS()
{
try
{
// Create a ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo("cmd","");
// Set up the values
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.CreateNoWindow = false;
// Create a cmd.exe process
Process process = new Process();
// Apply the start info and run
process.StartInfo = startInfo;
process.Start();
return process;
}
catch (Exception)
{
return null;
}
}
Method that formats the response into a string :
private string findResponse(StreamReader err,StreamReader outp)
{
string output = outp.ReadToEnd();
string error = err.ReadToEnd();
string ret = "";
// Add the output to the return field if the process actually output data
if (output.Length > 0)
{
ret = "OUTPUT : " + output;
}
// Then attempt to add data from the error stream
if (error.Length > 0)
{
// If output was read, add a newline character separating the two fields
if (ret.Length > 0) ret = ret + Environment.NewLine;
ret = ret + "ERROR : " + error;
}
// If there's no output, that means there's no error, which means the execution was silently succesful
if (ret.Length <= 0)
{
ret = "Command execution succesful!";
}
return ret;
}
Server listener block :
private void run()
{
while (true)
{
Stream stream = null;
StreamReader sr = null;
StreamWriter sw = null;
Socket socket = null;
Process proc = null;
try
{
socket = server.AcceptSocket();
stream = new NetworkStream(socket);
sr = new StreamReader(stream);
sw = new StreamWriter(stream);
String mode = sr.ReadLine();
sw.WriteLine("Serverside link connected");
sw.Flush();
// Create cmd process in WINPROCESS mode
if (mode == "WINPROCESS")
{
proc = createDOS();
}
String line;
while ((line = sr.ReadLine()) != null)
{
if (mode == "WINPROCESS")
{
proc.StandardInput.WriteLine(line);
proc.StandardInput.Flush();
// Response
sw.WriteLine(findResponse(proc.StandardError, proc.StandardOutput));
sw.Flush();
}
else
{
sw.WriteLine(exeDOS(line));
sw.Flush();
}
}
}
catch (Exception ex)
{
// Silence
}
finally
{
if (socket != null) socket.Close();
if (stream != null) stream.Close();
if (sw != null) sw.Close();
if (sr != null) sr.Close();
if (proc != null) proc.Close();
}
}
}
Any help would be appreciated, thanks!
You need to add a handler to the OutputDataReceived and ErrorDataReceived events:
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived +=
new DataReceivedEventHandler(process_OutputDataReceived);
process.StartInfo.RedirectStandardError = true;
process.ErrorDataReceived +=
new DataReceivedEventHandler(process_ErrorDataReceived);
if (process.Start())
{
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
}
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
I am reading the output of a java application started using Process and reading stdError, stdOutout and using stdInput to send commands. Here is the relevant code:
int mem = Properties.Settings.Default.mem_max;
string locale = Properties.Settings.Default.location;
Process bukkit_jva = new Process();
bukkit_jva.StartInfo.FileName = "java";
//bukkit_jva.StartInfo.Arguments = "-Xmx" + mem + "M -Xms" + mem + "M -jar " + locale + "bukkit.jar";
bukkit_jva.StartInfo.Arguments = "-Xmx512M -Xms512M -jar C:\\bukkit\\bukkit.jar";
bukkit_jva.StartInfo.UseShellExecute = false;
bukkit_jva.StartInfo.CreateNoWindow = true;
bukkit_jva.StartInfo.RedirectStandardError = true;
bukkit_jva.StartInfo.RedirectStandardOutput = true;
bukkit_jva.StartInfo.RedirectStandardInput = true;
bukkit_jva.Start();
//start reading output
SetText(bukkit_jva.StandardOutput.ReadLine());
SetText(bukkit_jva.StandardOutput.ReadLine());
SetText(bukkit_jva.StandardOutput.ReadLine());
SetText(bukkit_jva.StandardOutput.ReadLine());
StreamReader err = bukkit_jva.StandardError;
StreamReader output = bukkit_jva.StandardOutput;
StreamWriter writer = bukkit_jva.StandardInput;
SetText(err.Peek().ToString());
while (false == false)
{
if (vars.input != null)
{
writer.WriteLine(vars.input);
vars.input = null;
}
SetText(output.ReadLine() + err.ReadLine());
}
}
SetText() adds the line to a list of lines.
My problem is that the java app sometimes returns a string even when there is no input, so I always need to check for a new line. but If I need to send a command, and there is no new output, It will not send.
I tried different If statements on the readline, but it would only return the first few lines then it would stop.
basicly it seems to pause the loop if there is no new line for it to read.
How could I either setup my read/write loop differently or get the loop to unpause?
Thanks,
Adam
try this:
static void Main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo("echoApp.exe");
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process echoApp = new Process();
echoApp.ErrorDataReceived += new DataReceivedEventHandler(echoApp_ErrorDataReceived);
echoApp.OutputDataReceived += new DataReceivedEventHandler(echoApp_OutputDataReceived);
echoApp.StartInfo = psi;
echoApp.Start();
echoApp.BeginOutputReadLine();
echoApp.BeginErrorReadLine();
echoApp.StandardInput.AutoFlush = true;
string str = "";
while (str != "end")
{
str = Console.ReadLine();
echoApp.StandardInput.WriteLine(str);
}
echoApp.CancelOutputRead();
echoApp.CancelErrorRead();
echoApp.Close();
}
static void echoApp_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("stdout: {0}", e.Data);
}
static void echoApp_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("stderr: {0}", e.Data);
}
and the little echoApp...
//echoApp
static void Main(string[] args)
{
string str="";
while (str != "end")
{
str = Console.ReadLine();
Console.WriteLine(str);
}
}
Please note if you try to access the output after CancelOutputRead() and CancelErrorRead() you may find that occasionally you are missing some text. I found the flush only occurs after explicitly calling Close(). Disposing (with a using statement) doesn't help.
This symptom will most likely occur when calling the command processor (CMD.EXE) because it does not explicitly flush itself. So be careful not to try and access the output (written from your event handlers) unless you explicitly call Close() first.