public Search(string SourcePath, CallBackSearchFolder callback, ShowMSGDelegate showmsg) { this._sourcepPath = SourcePath; this._callbackSearch = callback; this._show_msg_callback = showmsg; MakeDataTable(); }
//异步方式 public void printInfo() { Console.WriteLine("MainThread ManagedThreadId is :" + System.Threading.Thread.CurrentThread.ManagedThreadId); //初始化委托 ComputeDelegate = new ShowMSGDelegate(Fibonacci); //声明并初始化一个异步回调,完成后会调用CheckedCompeleted AsyncCallback callback = new AsyncCallback(CheckedCompeleted); //开始多线程调用 IAsyncResult sayncResult = ComputeDelegate.BeginInvoke(9, callback, "hi,this is customize message!"); System.Threading.Thread.Sleep(1000); Console.WriteLine("Done!"); Console.ReadKey(); }
//阻塞当前线程 public void printInfo3() { Console.WriteLine("MainThread ManagedThreadId is :" + System.Threading.Thread.CurrentThread.ManagedThreadId); //初始化委托 ComputeDelegate = new ShowMSGDelegate(Fibonacci); //声明并初始化一个异步回调,完成后会调用CheckedCompeleted IAsyncResult sayncResult = ComputeDelegate.BeginInvoke(9, null, null); //阻塞当前线程,等待子线程调用结束 sayncResult.AsyncWaitHandle.WaitOne(); ////结束委托调用,返回子线程调用结果 int[] callResult = ComputeDelegate.EndInvoke(sayncResult); Console.WriteLine("Last value:" + callResult[0]); Console.WriteLine("Done!"); Console.ReadKey(); }
private void StartSearch() { #region Start Search // Get SourcePath if (GetSourceFolder() == false) { return; } // // Clear ListView dtFiles.Clear(); // // If Search was canceled last time reset flag Search.CancelSearch = false; // if (CheckBoxSearchSubFolder.IsChecked == false) { // Search in New Thread CallBackSearchFolder cbSearch = new CallBackSearchFolder(BindAsync); ShowMSGDelegate shd = new ShowMSGDelegate(ShowMSG); Search sh = new Search(SourcePath, cbSearch, shd); ThreadStart ts = new ThreadStart(sh.SearchFolderNewThread); Thread t1 = new Thread(ts); t1.IsBackground = true; t1.Start(); // } else if (CheckBoxSearchSubFolder.IsChecked == true) { CallBackSearchFolder cbSearch = new CallBackSearchFolder(BindAsync); ProgressCurrentFile pcFile = new ProgressCurrentFile(ShowFoundCurrentFile); ShowMSGDelegate shd = new ShowMSGDelegate(ShowMSG); Search sh = new Search(SourcePath, cbSearch, pcFile, shd); ThreadStart ts = new ThreadStart(sh.SearchSubFolderNewThread); Thread t1 = new Thread(ts); t1.IsBackground = true; t1.Start(); } #endregion }
//同步方式 public void printInfo2() { Console.WriteLine("MainThread ManagedThreadId is :" + System.Threading.Thread.CurrentThread.ManagedThreadId); //初始化委托 ComputeDelegate = new ShowMSGDelegate(Fibonacci); //声明并初始化一个异步回调,完成后会调用CheckedCompeleted IAsyncResult sayncResult = ComputeDelegate.BeginInvoke(9, null, null); //判断子线程是否完成,异步方式 while (!sayncResult.IsCompleted) { //do other something Console.WriteLine("Current Value:" + WatchVarInter); } //结束委托调用,返回子线程调用结果 int[] callResult = ComputeDelegate.EndInvoke(sayncResult); Console.WriteLine("Last value:" + callResult[0]); Console.WriteLine("Done!"); Console.ReadKey(); }