EventWaitHandle使用方法小记
发布日期:2021-05-14 04:37:35 浏览次数:15 分类:博客文章

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

���������������������������������������������������������������������������������������������������������������������������������������������������BeginInvoke���������EndInvoke������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

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

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

//start to perform async action         private void BeginDealDataInformation(ListView listview,string fileName,EnumX enums)         {             DealDataInformationDelegateAsync dAsync = new DealDataInformationDelegateAsync(DealDataInformation);             IAsyncResult iar = dAsync.BeginInvoke(listview,fileName,enums,new AsyncCallback(EndDealDataInformation),dAsync);                         if ((enumThis = enums) == EnumX.leftSide) tsStatus.Text = "���������������������������������������������...";             if ((enumThis = enums) == EnumX.rightSide) tsStatus.Text = "���������������������������������������������...";         }         //returen the async running result         private void EndDealDataInformation(IAsyncResult iar)         {             UIThread.UIInvoke(this.toolStrip1, delegate             {                 if (enumThis == EnumX.leftSide) tsStatus.Text = "������������������������������������������������...";                 if (enumThis == EnumX.rightSide) tsStatus.Text = "������������������������������������������������...";             });             DealDataInformationDelegateAsync dAsync = (DealDataInformationDelegateAsync)iar.AsyncState;             dAsync.EndInvoke(iar);         }         //this is a function that takes a lot of time to run, need to use async action to perform it.         private void DealDataInformation(ListView listview,string fileName,EnumX enums)         {             //myHandle.Reset();//������������������������������������             if (ColumnHeaderCollection == null) throw new Exception("���������������������������������������������������");             int columnCount = ColumnHeaderCollection.Length;             for (int i = 0; i < columnCount; i++)             {                 ColumnHeader header = new ColumnHeader();                 header.Text = ColumnHeaderCollection[i];                 header.TextAlign = HorizontalAlignment.Left;                 UIThread.UIInvoke(listview, delegate {                     listview.Columns.Add(header);                               });             }             int tryResult;             using (StreamReader sr = new StreamReader(fileName, Encoding.Default))             {                 string result;                 while ((result = sr.ReadLine()) != null)                 {                     string[] _result = result.Split(',');                     ListViewItem lvi = new ListViewItem(_result[0]);                     if (Int32.TryParse(_result[0], out tryResult))                     {                         if (enums == EnumX.leftSide) DataCache.cacheRecordListOne.Add(Int32.Parse(_result[0]));  //cache the record                         if (enums == EnumX.rightSide) DataCache.cacheRecordListTwo.Add(Int32.Parse(_result[0]));  //cache the record                     }                     for (int j = 1; j < _result.Length; j++)                     {                         lvi.SubItems.Add(_result[j]);                     }                     //add items                     UIThread.UIInvoke(listview, delegate                     {                         listview.Items.Add(lvi);                         if (enums == EnumX.leftSide)  DataCache.listItemLeft.Add(lvi);                         if (enums == EnumX.rightSide) DataCache.listItemRight.Add(lvi);                     });                 }             }             //remove the first item(commonly, it's a header)             UIThread.UIInvoke(listview, delegate             {                 listview.Items.RemoveAt(0);                 //DataCache.listItemLeft.RemoveAt(0);                 //DataCache.listItemRight.RemoveAt(0);             });             //myHandle.Set();  //���������������������������������         }

 ������������������������������������3������������

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

private void ColorTheResultAsync(EnumX enums,ListView listview)         {             //myHandle.WaitOne(); //���������������������������������Set���������             CommonUntil commonUntil = new CommonUntil();             List
existList=null; List
nonExistList = null; if (enums == EnumX.leftSide) { existList = commonUntil.ReturnComparisonResult(DataCache.cacheRecordListOne, DataCache.cacheRecordListTwo)[0]; nonExistList=commonUntil.ReturnComparisonResult(DataCache.cacheRecordListOne, DataCache.cacheRecordListTwo)[1]; listPartOne[0] = existList; listPartOne[1] = nonExistList; } if (enums == EnumX.rightSide) { existList = commonUntil.ReturnComparisonResult(DataCache.cacheRecordListTwo, DataCache.cacheRecordListOne)[0]; nonExistList = commonUntil.ReturnComparisonResult(DataCache.cacheRecordListTwo, DataCache.cacheRecordListOne)[1]; listPartTwo[0] = existList; listPartTwo[1] = nonExistList; } int __result; UIThread.UIInvoke(listview, delegate { foreach (ListViewItem lvi in listview.Items) { if (Int32.TryParse(lvi.Text, out __result)) { if (existList.Contains(Int32.Parse(lvi.Text))) { if (enums == EnumX.leftSide) lvi.BackColor = Color.Wheat; if (enums == EnumX.rightSide) lvi.BackColor = Color.YellowGreen; } } } listview.Invalidate(); }); } private void BeginColorTheResultAsync(EnumX enums,ListView listview) { ColorTheResultDelegateAsync colorTheResultAsync = new ColorTheResultDelegateAsync(ColorTheResultAsync); IAsyncResult iar = colorTheResultAsync.BeginInvoke(enums, listview, new AsyncCallback(EndColorTheResultAsync),colorTheResultAsync); tsStatus.Text = "���������������������������..."; } private void EndColorTheResultAsync(IAsyncResult iar) { tsStatus.Text = "������������������������..."; ColorTheResultDelegateAsync colorTheResultAsync = (ColorTheResultDelegateAsync)iar.AsyncState; colorTheResultAsync.EndInvoke(iar); }

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

���������������������������������������������������Thread���join������������������������������������������������������������������������������������������������������������������������������������������ParameterizedThreadStart������������������������������object������������������������������������������������������������(������������������������������������������������������object������������������������������������������������������������������������������������������������)������������������������������������

������������������������������������������������������������EventWaitHandle������������������������������

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

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

EventWaitHandle myHandle = new EventWaitHandle(false, EventResetMode.ManualReset);  //���������������������������������������������������

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

myHandle.Reset();//������������������������������������

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

myHandle.Set();  //���������������������������������

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

private void DealDataInformation(ListView listview,string fileName,EnumX enums)  {        myHandle.Reset();//������������������������������������ ������������....................        myHandle.Set();  //���������������������������������   }

������������Reset������������������������������������������������������������������������������������������������������������������������Set������������������������������������������������������������������������������������������������

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

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

private void ColorTheResultAsync(EnumX enums,ListView listview) {         myHandle.WaitOne(); //���������������������������������Set���������       ......... }

������������������������DealDataInformation������������������������������������ColorTheResultAsync������������������������������������������������������������������������

上一篇:Excel的xml表现格式
下一篇:正则表达式点滴(2)

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月11日 04时01分34秒