Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey every body, can help me about this error.
It's my code:
C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
       {
           string date="";
           Int64  m_totalSize=0;
             for (int i = 0; i < listVSearch.Items.Count; i++)
           {
               try
               {
                   m_totalSize=0;
                   HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(listVSearch.Items[i].SubItems[3].Text);
                    HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
                   DateTime today = DateTime.Now;
                   if (DateTime.Compare(today, webResp.LastModified) == 1)
                     date = webResp.LastModified.ToShortDateString();
                    m_totalSize += webResp.ContentLength;
                     webResp.Close();
               }
               catch (Exception) { }
                 listVSearch.Items[i].SubItems[2].Text=date.ToString();
                 listVSearch.Items[i].SubItems[1].Text = (FormatSizeDecimal(m_totalSize, 2)).ToString();
           }
       }


I'm call backgroundWorker1.RunWorkerAsync() affter event:
C#
 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
...
backgroundWorker1.RunWorkerAsync();
}

..THen view error:
Please help me,hjxhjx, thank all!
Posted

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
You should not access a form control within the DoWork event. It can be done but is not recommended. Populate the list view in the complete event not the DoWork Event.

if you really have to access it from the dowork event see this link :
[Look Here]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900