跨线程操作无效:帮帮我吗? (请问我的英语,我来自越南语) [英] Cross-thread operation not valid: Help me? (sr my english, i'm from Vietnamese)

查看:82
本文介绍了跨线程操作无效:帮帮我吗? (请问我的英语,我来自越南语)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,可以帮我解决这个错误.
这是我的代码:

hey every body, can help me about this error.
It''s my code:

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();
           }
       }



我将其称为backgroundWorker1.RunWorkerAsync()之后事件:



I''m call backgroundWorker1.RunWorkerAsync() affter event:

 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
...
backgroundWorker1.RunWorkerAsync();
}


..查看错误:
请帮助我,谢谢!


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

推荐答案

您不能从非UI线程调用与UI相关的任何内容.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

-SA
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


您不应在DoWork事件中访问表单控件.可以做到,但不建议这样做.在完整事件而非DoWork事件中填充列表视图.

如果确实需要通过dowork事件访问它,请参见以下链接:
[看这里]
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]


这篇关于跨线程操作无效:帮帮我吗? (请问我的英语,我来自越南语)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆