Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Thread issues

Tags:

c#

What I have going on is a listview being dynamically created from a previous button click. Then ti starts a background worker in which should clear out the listview and populate the iistview with new information every 30 seconds.
I continously get: Cross-thread operation not valid: Control 'listView2' accessed from a thread other than the thread it was created on.

  private void watcherprocess1Updatelist()
    {
        listView2.Items.Clear();
        string betaFilePath1 = @"C:\Alpha\watch1\watch1config.txt";
        using (FileStream fs = new FileStream(betaFilePath1, FileMode.Open))
        using (StreamReader rdr = new StreamReader((fs)))
        {
            while (!rdr.EndOfStream)
            {
                string[] betaFileLine = rdr.ReadLine().Split(',');
                using (WebClient webClient = new WebClient())
                {
                    string urlstatelevel = betaFileLine[0];
                    string html = webClient.DownloadString(urlstatelevel);
                    File.AppendAllText(@"C:\Alpha\watch1\specificconfig.txt", html);
                }
            }
        }

2 Answers

You cannot access your ListView from the background thread. You need to do this on the UI thread.

There are two options - first, you can switch this around to use a Windows.Forms.Timer to fire every 30 seconds. This will occur on the UI thread, which will avoid this problem entirely, but move the processing onto the UI thread. If your processing is slow, this could cause "hangs".

Alternatively, use Control.Invoke to marshal the calls to ListView back onto the UI thread:

 listView2.Invoke(new Action( () => listView2.Items.Clear() ) );

In any case, I'd rethink using a BackgroundWorker. It's not intended for "timed events" that happen on regular intervals. You should consider changing to a Windows.Forms.Timer (UI thread) if there will be no long processing, or a System.Timers.Timer (runs on a background thread) if the processing will take a while. This is a better design option than a BackgroundWorker which never terminates.

like image 61
Reed Copsey Avatar answered Mar 16 '26 21:03

Reed Copsey


You need to use Control.Invoke to call code on the UI thread to update the listView.

like image 28
Justin Ethier Avatar answered Mar 16 '26 20:03

Justin Ethier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!