开发者

Assigning a property across threads

I have set a property across threads before and I found this post Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on about getting a property.

I think my issue with the code below i开发者_高级运维s setting the variable to the collection is an object therefore on the heap and therefore is just creating a pointer to the same object

So my question is besides creating a deep copy, or copying the collection into a different List object is there a better way to do the following to aviod the error during the for loop.

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

Code:

    private void btnRunProcess_Click(object sender, EventArgs e)
    {
        richTextBox1.Clear();

        BackgroundWorker bg = new BackgroundWorker();
        bg.DoWork += new DoWorkEventHandler(bg_DoWork);
        bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
        bg.RunWorkerAsync();
   }


    void bg_DoWork(object sender, DoWorkEventArgs e)
    {
        WorkflowEngine engine = new WorkflowEngine();
        ListBox.SelectedObjectCollection selectedCollection=null;

        if (lstProcessFiles.InvokeRequired)
        {
            // Try #1
            selectedCollection = (ListBox.SelectedObjectCollection) 
                this.Invoke(new GetSelectedItemsDelegate(GetSelectedItems), 
                new object[] { lstProcessFiles });

            // Try #2
            //lstProcessFiles.Invoke(
            //    new MethodInvoker(delegate {
            //        selectedCollection = lstProcessFiles.SelectedItems; }));
        }
        else
        {
            selectedCollection = lstProcessFiles.SelectedItems;
        }         

        // *********Same Error on this line********************
        // Cross-thread operation not valid: Control 'lstProcessFiles' accessed 
        // from a thread other than the thread it was created on.
        foreach (string l in selectedCollection)
        {
            if (engine.LoadProcessDocument(String.Format(@"C:\TestDirectory\{0}", l)))
            {
                try
                {
                    engine.Run();                       
                    WriteStep(String.Format("Ran {0} Succussfully", l));
                }
                catch
                {
                    WriteStep(String.Format("{0} Failed", l));
                }

                engine.PrintProcess();
                WriteStep(String.Format("Rrinted {0} to debug", l));
            }
        }
    }

    private delegate void WriteDelegate(string p);
    private delegate ListBox.SelectedObjectCollection GetSelectedItemsDelegate(ListBox list);

    private ListBox.SelectedObjectCollection GetSelectedItems(ListBox list)
    {
        return list.SelectedItems;
    }


Take a look at this SO question - it addresses a similar topic.

In many UI technologies (Winforms, WPF, Silverlight) - UI elements can only be safely interacted with on the UI thread. This means that when writing multithreaded code, you need to use the mechanisms in your UI library of choice to correctly interact with UI controls. In WPF/Silverlight that would be the Dispatcher, in WinForms it requires using InvokeRequired method and BeginInvoke() to dispatch the work to the UI thread.

In your case, it seems like you are already trying to use BeginInvoke to dispatch to the appropriate thread. I suspect the problem is that iterating over the ListBox's collection is a cross-thread operation itself. You don't know how the SelectedItems collection implements GetEnumerator() - most likley though it doesn't copy the collection. So I suspect your code will have to make a copy before iterating over it - or perform the entire iteration on the UI thread.

Making a copy of the collection isn't to bad, but again, the copy has to be made on the UI thread - and it may need to be a deep copy since it contains other UI-owned objects (ListBoxItem). If you are on .NET 3.5, you can use LINQ to project an anonymous object in your code rather than trying to make deep copies of UI elements.


You have to use the InvokeRequired property of the Control to avoid calling one control from a different thread.

Check for example this page


You are alrady passing the selected items in this line:

bg.RunWorkerAsync(lstProcessFiles.SelectedItems);

why are you trying to get them again in the DoWork method?

Access them from the DoworkEventArgs with:

var collection = (ListBox.SelectedObjectCollection)e.Argument

(You may still need to copy the selected objects into a plain list before you call the background worker, I'm not sure what objects live in that specialised collection type)


I just made a copy of the string object like so. Even if it was a more complicated object something like this should still work

private List<string> GetSelectedItems(ListBox list)
{
    return lstProcessFiles.SelectedItems.Cast<string>().ToList();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜