1

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!

user3010406
  • 541
  • 2
  • 8
  • 22

1 Answers1

1

You can use a Dictionary and connect the Cookie Of The User with the message that can read.

public static Dictionary<string, string> messages = new Dictionary<string, string>();

The key, must be the user cookie.

But this is not a bug free solution.

Bug number 1, on the recycle of the pool you lose your data.
Bug number 2, on any update/compile of your web site, your lose your data.
Bug number 3, when you have more than one pools (web garden) each pool have their static data, so the same user can be lost/never see their data.

The correct way is to use a database, either some file to write them down - and connect the messages with the user cookie / user id

Lifetime of ASP.NET Static Variable

Aristos
  • 66,005
  • 16
  • 114
  • 150