-1

我正在使用后台工作人员循环遍历 a 中的每个项目,ListView并在单击按钮后对其进行处理:

private void bParsePosts_Click(object sender, EventArgs e)
{
    parseWorker.RunWorkerAsync(this.lvPostQueue);
}

然后,我有:

private void parseWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // Loop through each item
    for (int i = 0; i < lvPostQueue.Items.Count; i++)
    {
        string title = lvPostQueue.Items[i].SubItems[0].ToString();
        string category = lvPostQueue.Items[i].SubItems[1].ToString();
        string url = lvPostQueue.Items[i].SubItems[2].ToString();

        lvPostQueue.Items[i].SubItems[3].Text = "Done";
    }
}

但是,我收到此错误:

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

我将如何lvPostQueue从该后台工作人员中操作控件?

谢谢。

4

2 回答 2

0

只需使用线程安全调用:http: //msdn.microsoft.com/en-us/library/ms171728.aspx

例子:

// This event handler starts the form's  
        // BackgroundWorker by calling RunWorkerAsync. 
        // 
        // The Text property of the TextBox control is set 
        // when the BackgroundWorker raises the RunWorkerCompleted 
        // event. 
        private void setTextBackgroundWorkerBtn_Click(
            object sender, 
            EventArgs e)
        {
            this.backgroundWorker1.RunWorkerAsync();
        }

        // This event handler sets the Text property of the TextBox 
        // control. It is called on the thread that created the  
        // TextBox control, so the call is thread-safe. 
        // 
        // BackgroundWorker is the preferred way to perform asynchronous 
        // operations. 

        private void backgroundWorker1_RunWorkerCompleted(
            object sender, 
            RunWorkerCompletedEventArgs e)
        {
            this.textBox1.Text = 
                "This text was set safely by BackgroundWorker.";
        }
于 2012-08-26T00:00:29.353 回答
0

正确的答案是:

private void bParsePosts_Click(object sender, EventArgs e)
{
    parseWorker.WorkerReportsProgress = true;
    parseWorker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
    parseWorker.RunWorkerAsync();
}

private void parseWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // Loop through each item
    for (int i = 0; i < lvPostQueue.Items.Count; i++)
    {
        string title = lvPostQueue.Items[i].SubItems[0].ToString();
        string category = lvPostQueue.Items[i].SubItems[1].ToString();
        string url = lvPostQueue.Items[i].SubItems[2].ToString();

        parseWorker.ReportProgress(i * 100 / lvPostQueue.Items.Count, i);
    }
}

void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    var i = (int)e.UserState;
    lvPostQueue.Items[i].SubItems[3].Text = "Done";
}
于 2012-08-26T06:18:33.270 回答