委托BegionInvoke和窗體BegionInvoke

委托BegionInvoke是指通過委托方法執行多線程任務,例如:

//定義委托成員變量

delegate? void dg_DeleAirport();

//指定委托函數

dg_DeleAirport dga = AirportBLL.DeleteHistoryTransAirport;

//通過BeginInvoke以異步線程方式執行委托函數,可通過EndInvoke獲取返回值

//如果委托函數需要傳入參數,這些傳入參數寫在下面的兩個null之前,

//這兩個參數,第一個可指明回調函數名稱(函數本身可由參數,但這里不寫參數),就是異步線程結束后主線程執行的函數,參數一般就是asrs,以便主線程處理返回結果,回調函數一般諸如xxxCompleted,且必須帶參數(IAsyncResult asrs),回調函數如:

void DelAirportCompleted(IAsyncResult df)
{
  ???? if (df != null)
??????????? {
??????????????? string cc=(df.AsyncState as dg_DeleAirport).EndInvoke(df).ToString();
??????????? }
??????????? inprocess = false;
}

//第二個參數表示第一個參數代表的值,往往就是調用者本身,如下句的dga。

IAsyncResult asrs = dga.BeginInvoke(null, null);

//此時,主線程將直接執行后續代碼,不會等待委托函數執行完畢。但是,可以通過EndInvoke或asrs.IsCompleted等阻塞函數進行強制等待委托函數完成,

//EndInvoke()函數可稱為阻塞函數,阻止主進程繼續往下進行直到異步線程完成。

//EndInvoke取回委托函數返回值,返回類型跟委托的類型一樣,如果委托函數返回類型是void,此函數的返回值也是void

while(!asrs.IsCompleted())一樣也可以起到強制主線程等待的作用,而且還可以在while方法中讓主線程做些等待相關的操作

dga.EndInvoke(asrs);

while (!asrs.IsCompleted)
{
}

此外,還可通過asrs.AsyncWaitHandle.WaitOne(5000, false);設置等待時間,無論是否完成,在等待時間后,繼續往下進行。第一個參數表示等待毫秒數時間,在指定時間內如果異步線程完成,返回true,否則false。

不建議使用阻塞函數,因為主界面會假死,跟不用多線程沒有區別。

轉載于:https://www.cnblogs.com/mol1995/p/7614820.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540542.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540542.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540542.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

圖論 弦_混亂的弦

圖論 弦Problem statement: 問題陳述: You are provided an input string S and the string "includehelp". You need to figure out all possible subsequences "includehelp" in the string S? Find out the number of ways in which the s…

[轉載] Python列表操作

參考鏈接: Python中的基本運算符 Python列表: 序列是Python中最基本的數據結構。序列中的每個元素都分配一個數字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推; Python有6個序列的…

「原創」從馬云、馬化騰、李彥宏的對話,看出三人智慧差在哪里?

在今年中國IT領袖峰會上,馬云、馬化騰、李彥宏第一次單獨合影,同框畫面可以說很難得了。BAT關心的走勢一直是同行們競相捕捉的熱點,所以三位大Boss在這次大會上關于人工智能的見解,也受到廣泛關注與多方解讀。馬云認為機器比人聰明…

python 注釋含注釋_Python注釋

python 注釋含注釋Python注釋 (Python comments) Comments in Python are used to improve the readability of the code. It is useful information given by the programmer in source code for a better understanding of code and logic that they have used to solve the …

C2的完整形式是什么?

C2:核心2 (C2: Core 2) C2 is an abbreviation of "Core 2" or "Intel Core 2". C2是“ Core 2”或“ Intel Core 2”的縮寫 。 It is a family of Intels processor which was launched on the 27th of July, 2006. It comprises a series of…

scala特性_Scala | 特性應用

scala特性特性應用 (Trait App) Scala uses a trait called "App" which is used to convert objects into feasible programs. This conversion is done using the DelayedInit and the objects are inheriting the trait named App will be using this function. T…

[轉載] Python3中的表達式運算符

參考鏈接: Python中的除法運算符 1:Python常用表達式運算符 yield 生成器函數send協議 lambda args:expression 創建匿名函數 x if y else z 三元選擇表達式(當y為真時,x才會被計算) x or y 邏輯或(僅但x為假時y才會被計算) x and …

字符串矩陣轉換成長字符串_字符串矩陣

字符串矩陣轉換成長字符串Description: 描述: In this article, we are going to see how backtracking can be used to solve following problems? 在本文中,我們將看到如何使用回溯來解決以下問題? Problem statement: 問題陳述&#xf…

pythonchallenge_level2

level2 地址:http://www.pythonchallenge.com/pc/def/ocr.html。 源碼:gitcode.aliyun.com:qianlizhixing12/PythonChallenge.git。 問題:找出頁面源碼一點提示注釋中的稀有字符。 #!/usr/bin/env python3 # -*- coding:UTF-8 -*-# Level 2im…

[轉載] python類運算符的重載

參考鏈接: Python中的運算符重載 alist input().split() blist input().split() n float(input()) class Vector: def __init__(self, x0, y0, z0): # 請在此編寫你的代碼(可刪除pass語句) self.X x self.Y y self.Z z # 代碼結束 def __add__(self, other):…

r語言 運算符_R語言運算符

r語言 運算符R語言中的運算符 (Operators in R Language) Generally speaking, an operator is a symbol that gives proper commands to the compiler regarding a specific action to be executed. The operators are used for carrying out the mathematical or logical cal…

[轉載] Python基礎之類型轉換與算術運算符

參考鏈接: Python中的運算符函數| 1 一、注釋 1.注釋:對程序進行標注和說明,增加程序的可讀性。程序運行的時候會自動忽略注釋。 2.單行注釋:使用#的形式。但是#的形式只能注釋一行,如果有多行,就不方便…

java awt 按鈕響應_Java AWT按鈕

java awt 按鈕響應The Button class is used to implement a GUI push button. It has a label and generates an event, whenever it is clicked. As mentioned in previous sections, it extends the Component class and implements the Accessible interface. Button類用于…

解決“由于應用程序的配置不正確,應用程序未能啟動,重新安裝應用程序可能會糾正這個問題”...

在VS2005下用C寫的程序,在一臺未安裝VS2005的系統上, 用命令行方式運行,提示: “系統無法執行指定的程序” 直接雙擊運行,提示: “由于應用程序的配置不正確,應用程序未能啟動,重新安…

qgis在地圖上畫導航線_在Laravel中的航線

qgis在地圖上畫導航線For further process we need to know something about it, 為了進一步處理,我們需要了解一些有關它的信息, The route is a core part in Laravel because it maps the controller for sending a request which is automatically …

Logistic回歸和SVM的異同

這個問題在最近面試的時候被問了幾次,讓談一下Logistic回歸(以下簡稱LR)和SVM的異同。由于之前沒有對比分析過,而且不知道從哪個角度去分析,一時語塞,只能不知為不知。 現在對這二者做一個對比分析&#xf…

[轉載] python學習筆記2--操作符,數據類型和內置功能

參考鏈接: Python中的Inplace運算符| 1(iadd(),isub(),iconcat()…) 什么是操作符? 簡單的回答可以使用表達式4 5等于9,在這里4和5被稱為操作數,被稱為操符。 Python語言支持操作者有以下幾種類型。 算…

scala bitset_Scala中的BitSet

scala bitsetScala BitSet (Scala BitSet) Set is a collection of unique elements. 集合是唯一元素的集合。 Bitset is a set of positive integers represented as a 64-bit word. 位集是一組表示為64位字的正整數。 Syntax: 句法: var bitset : Bitset Bits…