C# Thread issues
        Posted  
        
            by Mike
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Mike
        
        
        
        Published on 2010-04-05T17:29:02Z
        Indexed on 
            2010/04/05
            17:33 UTC
        
        
        Read the original article
        Hit count: 316
        
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);
                }
            }
        }
© Stack Overflow or respective owner