跨GUI线程拖放操作

时间:2013-08-19 21:07:36

标签: c# multithreading user-interface drag-and-drop

我有一个应用程序,目前有三种形式。其中两个表单具有网格控件,这些控件在不同的GUI线程上运行。我知道运行单独的gui线程不是常态,但性能要求迫使我采用这条路径。第三种形式在ListView框中有一组项目,可以在另外两种形式上拖放。我遇到的问题是实现这种拖放功能。我最初让这个应用程序在单个gui线程上运行,拖放工作正常。由于将它们分开,我遇到了跨线程操作的问题。这是我的代码示例。

private void grid1_DragDropContent(object sender, DragDropContentEventArgs e)
{
    if (e.Content == null)
    {

    if (e.Data.GetDataPresent(typeof(ListView.SelectedListViewItemCollection)))
    {
        e.Effect = DragDropEffects.Copy;

       ListView.SelectedListViewItemCollection items =                                  e.Data.GetData(typeof(ListView.SelectedListViewItemCollection)) as
                            ListView.SelectedListViewItemCollection;
         // Code Breaks Here
         foreach (ListViewItem item in items)
         {
             ......
         }

    }
  }
}

我尝试访问每个ListViewItem时遇到异常,因为它们是在不同的线程上创建的。我知道跨线程操作通常使用Invoke来解决,但我不确定如何在这里使用它,因为SelectedListViewItemCollection不允许我访问底层ListView。无论如何要解决这个问题?非常感谢所有帮助。

感谢。

0 个答案:

没有答案