异步调用之精简方式
发布日期:2021-05-14 04:37:32 浏览次数:17 分类:博客文章

本文共 3805 字,大约阅读时间需要 12 分钟。

������������Winform���������������������������������������UI���������������������������������������������������������������������������������

public delegate void AddListViewCrossThreadDelegate(******); public void AddListViewCrossThreads(ListViewItem lvi,int action) {        if (lsvName.InvokeRequired)        {             AddListViewCrossThreadDelegate d = new AddListViewCrossThreadDelegate(AddListViewCrossThreads);             lsvName.Invoke(addlistviewdelegate, lvi,action);        }        else        {             //���������������������������������������            } }

������������������������������������������������������������UI���������������������������������������������������������������������������������������������������������������������������������������������������������InvokeRequired���������������������������������������������������������������������������������������������������������������������������������Copy/Paste������������������������������������������������������������������������������������������������

���������������������������������������������������UI���������������������������������������������������������������������(���������������������������InvokeRequired)���������������������������������������������������������������������������

using System.Windows.Forms; namespace CommonUntil {     public static class UIThread     {         public static void UIInvoke(this Control control, MethodInvoker invoker)         {             if (control.InvokeRequired)  //���������������������������������������             {                 control.Invoke(invoker); //������MethodInvoker������������������Delegate���������                    return;             }             else             {                 invoker.Invoke(); //������������������             }         }     } }

���������������������������������

������������������������������lsvName���ListView���������������������ListViewItem������������������������������������addFlag������������������������������������deleteFlag���������������������������������������������������

������������ public void AddListView(ListViewItem lvi,int action)         {             if (addFlag == action)             {                 this.lsvName.Items.Add(lvi);             }             else if (deleteFlag == action)             {                 this.lsvName.Items.Clear();             }         }

���������������������������������������������������������������������������������������ListViewItem���������action���������������������������������������������������������

������������������  for (int i = 0; i < myFiles.Count; i++)             {                 FileInfo file = myFiles[i];                 ListViewItem lvi = new ListViewItem();                 lvi.Text = GetFileName.GetFileName(file.FullName);                 lvi.Tag = file.FullName;  // store the fullname                 UIThread.UIInvoke(lsvName,                                   delegate                                   {                                       AddListView(lvi, addFlag);                                   }                 );             }

���������������������������������

UIThread.UIInvoke(lsvName,                    delegate                    {                             //AddListView(lvi, action);                             if (addFlag == action)                             {                                   this.lsvName.Items.Add(lvi);                             }                             else if (deleteFlag == action)                             {                                   this.lsvName.Items.Clear();                             }                    }  );

 ������������������������������������������������������������������������������������������������������������������������������������������

 

 

上一篇:让WebForm异步起来
下一篇:WCF 基础之契约(Contract)[转]

发表评论

最新留言

不错!
[***.144.177.141]2025年04月09日 02时59分38秒