1 #Region "Microsoft.VisualBasic::88599e270cb8b38339065a1d671ce31f, Microsoft.VisualBasic.Core\ApplicationServices\Parallel\Tasks\AsyncHandle.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class AsyncHandle
35     
36     '         Properties: Handle, IsCompleted, Task
37     
38     '         Constructor: (+1 OverloadsSub New
39     '         FunctionGetValue, Run
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Namespace Parallel.Tasks
47
48     ''' <summary>
49     ''' Represents the status of an asynchronous operation.(背景线程加载数据)
50     ''' </summary>
51     ''' <typeparam name="TOut"></typeparam>
52     ''' <remarks>
53     ''' 这个后台任务模块是为了更加方便的构建出匿名方法的调用过程,因为这个对象的
54     ''' 工作原理是基于匿名方法的``BeginInvoke``函数来工作的。
55     ''' </remarks>
56     Public Class AsyncHandle(Of TOut)
57
58         ''' <summary>
59         ''' 封装一个方法,该方法不具有参数,且返回由<typeparamref name="TOut"></typeparamref>参数指定的类型的值。
60         ''' </summary>
61         ''' <returns></returns>
62         Public ReadOnly Property Task As Func(Of TOut)
63         ''' <summary>
64         ''' 对后台任务的访问
65         ''' </summary>
66         ''' <returns></returns>
67         Public ReadOnly Property Handle As IAsyncResult
68
69         ''' <summary>
70         ''' Gets a value that indicates whether the asynchronous operation has completed.
71         ''' (获取一个值,该值指示异步操作是否已完成。)
72         ''' </summary>
73         ''' <returns></returns>
74         Public ReadOnly Property IsCompleted As Boolean
75             Get
76                 If Handle Is Nothing Then
77                     Return True
78                 End If
79
80                 Return Handle.IsCompleted
81             End Get
82         End Property
83
84         ''' <summary>
85         ''' Creates a new background task from a function handle.
86         ''' </summary>
87         ''' <param name="Task"></param>
88         Sub New(Task As Func(Of TOut))
89             Me.Task = Task
90         End Sub
91
92         ''' <summary>
93         ''' Start the background task thread.(启动后台背景线程)
94         ''' </summary>
95         ''' <returns></returns>
96         Public Function Run() As AsyncHandle(Of TOut)
97             If IsCompleted Then
98                 _Handle = Task.BeginInvoke(Nothing, Nothing' 假若没有执行完毕也调用的话,会改变handle
99             End If
100
101             Return Me
102         End Function
103
104         ''' <summary>
105         ''' Current thread will be blocked at here if the background task not finished.
106         ''' (没有完成的时候会一直在这里阻塞当前的线程)
107         ''' </summary>
108         ''' <returns></returns>
109         Public Function GetValue() As TOut
110             If Handle Is Nothing Then
111                 Return _Task()
112             Else
113                 Return Task.EndInvoke(Handle)
114             End If
115         End Function
116
117         Public Async Function GetValueAsyn() As Threading.Tasks.Task(Of TOut)
118             Return Await New Threading.Tasks.Task(Of TOut)(AddressOf GetValue)
119         End Function
120     End Class
121 End Namespace