I am starting a console application from an ASP.NET webform via the following, called from the Click event handler of a Button control:
Process p = new Process();
p.StartInfo.FileName = @"C:\HiImAConsoleApplication.exe";
// Set UseShellExecute to false for redirection.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.Arguments = "-u " + url + " -p BLAH";
p.StartInfo.CreateNoWindow = false;
// Set our event handler to asynchronously read the sort output.
p.OutputDataReceived += OutputReceived;
// Start the process.
p.Start();
// Start the asynchronous read of the sort output stream.
p.BeginOutputReadLine();
p.WaitForExit();
This works fine, I am using the OutputDataReceived event handler to read the output from the console application without issue by adding the received messages to a globally defined string collection, and then on a timer I get the new messages from a WebMethod.
protected static void OutputReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
messages.Add(myData);
}
if (messages.Count > 20)
{
messages.Clear();
}
}
and then to check for messages via the WebMethod:
public static List<string> messages = new List<string>();
[WebMethod]
public static string[] CheckForNewMessages()
{
List<string> tempCollection = new List<string>();
if (messages.ToArray().Length > 0)
{
foreach (string str in messages.ToArray())
{
tempCollection.Add(str);
}
}
return tempCollection.ToArray();
}
the problem with this approach is if I have more than one user attempt to use the application, they obviously share messages with each other and that's not very good. I am wondering if there is a better approach that would allow me to support multiple users more accurately.
TIA experts!