Cross Thread problem C#

Posted by Frederik Witte on Stack Overflow See other posts from Stack Overflow or by Frederik Witte
Published on 2011-02-19T15:10:01Z Indexed on 2011/02/19 15:25 UTC
Read the original article Hit count: 176

Filed under:
|
|
|

Hello people - I got this code (lg_log is a listbox, and i want it to log the start_server.bat) Here is the code i got:

public void bt_play_Click(object sender, EventArgs e)
        {
            lg_log.Items.Add("Starting Mineme server ..");

            string directory = Directory.GetCurrentDirectory();


            var info = new ProcessStartInfo(directory + @"\start_base.bat") {UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true, WorkingDirectory = directory + @"\Servers\Base"};
            var proc = new Process { StartInfo = info, EnableRaisingEvents = true };

            proc.OutputDataReceived += (obj, args) =>
            {
                if (args.Data != null)
                {
                    lg_log.Items.Add(args.Data);
                }
            };
            proc.Start();
            proc.BeginOutputReadLine();




            lg_log.Items.Add("Server is now running!");
            proc.WaitForExit();
        }

When i run this, i'll get an error .. Anybody can help me? I'll rate the answer up! :D Edit: The error i get is this: System.InvalidOperationException Hope it helps :)

The error comes at the lg_log.Items.Add(args.Data); code line

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading