Hi

I'm new to C# and need help. i want my listbox updated when it collect as many logs. The application at the background is still working but the GUI is not as responsive. I tried it with the thread also but got the error

"Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on."

Any help will be appreciated.

Thanx

Generally I try to keep my responses as positive and helpful as possible. While I'm not always able to give an actual solution I try to do what I can or I don't normally post at all.

However, in the past 24 hours of thread skimming I've come across approximately 75% of the posts I've looked at where no reference code is provided. While not every question requires code examples in order to be resolved, many would strongly benefit from them.

What would probably help here would be code snippets from the areas of your application in which the affected control is in play. By seeing how it is currently being manipulated perhaps one of the many skilled and experienced coders on this site will be able to spot a flaw or loophole and provide a solution for you.

Again, I apologize if this sounds harsh and I'm not 100% directing it at you... you just happened to be the one poster at the end of a long list of several who didn't provide any objective code reference to work with :P

Comments
Great Response!!!

You only want to use Invoke or BeginInvoke for the bare minimum piece of work required to change the UI.

Take a look at - http://www.yoda.arachsys.com/csharp/threads/ and http://www.yoda.arachsys.com/csharp/threads/winforms.shtml

You can place following code in click handler of button.

listBox1.Invoke(new Action(()=>
                {
                    for (int i = 1; i <= 10; i++)
                    {
                        System.Threading.Thread.Sleep(100);
                        listBox1.Items.Add(i.ToString());
                        listBox1.Refresh();
                    }

                }));

Please don't hesitate to post your code here.

private void FillLog(String sSyslog, String sFormIp)
        {
            String sPriority;

            sSyslog = sSyslog.Replace("\n", "");

            sSyslog = sSyslog.Substring(sSyslog.IndexOf(">") + 1);

            sSyslog = sSyslog.Trim();
            sPriority = GetSyslogPriority(sSyslog);

            StreamWriter swWriter = new StreamWriter(@"D:/syslogfiles/" + "syslog" + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year + DateTime.Now.Minute + ".txt", true);
            //listBox1.Items.Add(sFormIp + "," + sPriority + "," + sSyslog);
            // textBox1.AppendText(sFormIp + "," + sPriority + "," + sSyslog);
            Thread t = new Thread(new ThreadStart(addString));
            t.Start(sFormIp,sSyslog,sPriority);
            addString(sSyslog, sFormIp, sPriority);

            swWriter.WriteLine(sFormIp + "," + sPriority + "," + sSyslog);
            swWriter.Flush();
            swWriter.Close();
            
        }

        private void addString(String sFormIp, String sSyslog, String sPriority)
        {
            //for (int i = 0; i < 10; i++)
            //{
            //    //listBox1.Items.Add(sFormIp,sPriority,sSyslog);
            //    //textBox1.Text = (sFormIp+sPriority+sSyslog);
            //    //listBox1.Items.Add(sFormIp,sPriority,sSyslog);
            //    textBox1.Text = i.ToString();
            //    Thread.Sleep(100);
            //}

            //Invoke(new MethodInvoker(delegate { listBox1.Items.Add(sFormIp); }));

            listBox1.Invoke(new Action(() =>
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Thread.Sleep(100);
                        //listBox1.Items.Add(i.ToString());
                        //listBox1.Refresh();

                        textBox1.Text = (sFormIp + sSyslog+ sSyslog);
                    }
                }));
        }

This is my code snippet and is not a complete application. The FillLog method will get the logs and addstring method is used to update the listbox. The code you give is working but not in my situation. I want to fill the list box by sSyslog, sPriority, sFormIp. Now i'm getting the error "method name expected."

Plz Help
Thanx

This question has already been answered. Start a new discussion instead.