Server process running inside forms application with endless loop

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi

So.. I got this server program (a console program) running inside my form.. I have no problem starting it and hiding the window..
I set up a custom kinda prompt in my form (2 text boxes. One for server output and one for input)..
I have try doing this in 2 ways (not sure if any of them are the best or anything.. still not that good at C#)..
First way of starting the server.. This freezes the program due to the while loop, but not sure how to make it work the right way..

<div style="color:Black;background-color:White; <pre>
Process process = <span style="color:Blue; null;
StreamReader outputReader = <span style="color:Blue; null;
StreamWriter inputWriter = <span style="color:Blue; null;
StreamReader errorReader = <span style="color:Blue; null;

<span style="color:Blue; private <span style="color:Blue; void StartServer()
{
process = <span style="color:Blue; new Process
{
StartInfo =
{
FileName = <span style="color:#A31515; @"C:Serverserver.exe",
RedirectStandardOutput = <span style="color:Blue; true,
RedirectStandardError = <span style="color:Blue; true,
RedirectStandardInput = <span style="color:Blue; true,
CreateNoWindow = <span style="color:Blue; true,
WorkingDirectory = <span style="color:#A31515; @"C:Server",
UseShellExecute = <span style="color:Blue; false,
ErrorDialog = <span style="color:Blue; false
}
};
process.Start();
outputReader = process.StandardOutput;
inputWriter = process.StandardInput;
errorReader = process.StandardError;
<span style="color:Blue; string text = <span style="color:#A31515; "";
<span style="color:Blue; while (!process.HasExited)
{
text = outputReader.ReadLine();
<span style="color:Blue; if (text != <span style="color:Blue; null)
{
<span style="color:Blue; if (text == <span style="color:#A31515; "Server started")
{
<span style="color:Blue; continue;
}
<span style="color:Blue; else
{
<span style="color:Blue; if (text.Contains(<span style="color:#A31515; "Time:"))
{
<span style="color:Blue; continue;
}
rtxtServer.Text += text + <span style="color:#A31515; "n";
}
}
}
errorReader.Close();
errorReader = <span style="color:Blue; null;
inputWriter.Close();
inputWriter = <span style="color:Blue; null;
outputReader.Close();
outputReader = <span style="color:Blue; null;
serverrunning = <span style="color:Blue; false;
rtxtServer.Text += <span style="color:#A31515; "Server stopped" + <span style="color:#A31515; "n";
}
[/code]

I then tried using threads (for the first time) and I got it running with the while loop in the background just fine.. but when I try interacting with the server (sending a command through the inputstream) it says I cant do so across threads..
So.. I guess my questions is what way is the best to do this? And if its using threads: How do make my thread accept commands send from my forms application?
I hope you can help me out here :)

- Morten

View the full article
 
Back
Top