1 #Region "Microsoft.VisualBasic::49922bb572a6412da70ee516aa4d34f7, Microsoft.VisualBasic.Core\ApplicationServices\Parallel\Tasks\Task.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 Task
35     
36     '         Properties: Value
37     
38     '         Constructor: (+1 OverloadsSub New
39     
40     '         FunctionGetValue, Start
41     
42     '         Sub: __invokeTask
43     
44     '     Class Task
45     
46     '         Constructor: (+1 OverloadsSub New
47     
48     '         Function: Start
49     
50     '         Sub: __invokeTask, RaisingEvent
51     
52     '     Class Task
53     
54     '         Constructor: (+1 OverloadsSub New
55     
56     '         Function: Start
57     
58     '         Sub: __invokeTask, RaisingEvent
59     
60     
61     ' /********************************************************************************/
62
63 #End Region
64
65 Imports System.Reflection
66
67 Namespace Parallel.Tasks
68
69     ''' <summary>
70     ''' 更加底层的线程模式,和LINQ相比不会受到CPU核心数目的限制
71     ''' </summary>
72     ''' <typeparam name="T">后台任务的执行参数</typeparam>
73     ''' <typeparam name="TOut">后台任务的执行结果</typeparam>
74     Public Class Task(Of T, TOut) : Inherits IParallelTask
75
76         Dim _Handle As Func(Of T, TOut)
77         Dim _Input As T
78
79         ''' <summary>
80         ''' 假若任务已经完成,则会返回计算值,假若没有完成,则只会返回空值,假若想要在任何情况之下都会得到后台任务所执行的计算结果,请使用<see cref="GetValue()"/>方法
81         ''' </summary>
82         ''' <returns></returns>
83         Public ReadOnly Property Value As TOut
84
85         ''' <summary>
86         ''' 假若后台任务还没有完成,则函数会一直阻塞在这里直到任务执行完毕,假若任务早已完成,则函数会立即返回数据
87         ''' </summary>
88         ''' <returns></returns>
89         Public Function GetValue() As TOut
90             If Not Me.TaskRunning Then
91                 Call Start()
92             End If
93
94             Call WaitForExit()
95             Return _Value
96         End Function
97
98         Sub New(Input As T, Handle As Func(Of T, TOut))
99             _Handle = Handle
100             _Input = Input
101         End Sub
102
103         Public Function Start() As Task(Of T, TOut)
104             _TaskComplete = False
105             _RunningTask = True
106             Call New Threading.Thread(AddressOf __invokeTask).Start()
107             Return Me
108         End Function
109
110         Protected Overrides Sub __invokeTask()
111
112             _TaskComplete = False
113             _RunningTask = True
114             _Value = _Handle(_Input)
115             _TaskComplete = True
116             _RunningTask = False
117
118         End Sub
119     End Class
120
121     ''' <summary>
122     ''' 这个是带有<see cref="Task.TaskJobComplete"/>事件的任务对象
123     ''' </summary>
124     Public Class Task : Inherits IParallelTask
125
126         ''' <summary>
127         ''' 当所请求的任务执行完毕之后触发
128         ''' </summary>
129         Public Event TaskJobComplete()
130
131         Dim _Handle As Action
132
133         Sub New(Handle As Action)
134             _Handle = Handle
135         End Sub
136
137         Protected Overrides Sub __invokeTask()
138             _TaskComplete = False
139             _RunningTask = True
140             Call _Handle()
141             _TaskComplete = True
142             _RunningTask = False
143         End Sub
144
145         Public Function Start() As Task
146             _TaskComplete = False
147             _RunningTask = True
148             Call New Threading.Thread(AddressOf __invokeTask).Start()
149             Call Threading.Thread.Sleep(1)
150             Call New Threading.Thread(AddressOf RaisingEvent).Start()
151             Return Me
152         End Function
153
154         Private Sub RaisingEvent()
155             Call WaitForExit()
156             RaiseEvent TaskJobComplete()
157         End Sub
158     End Class
159
160     Public Class Task(Of T) : Inherits IParallelTask
161
162         Dim _Input As T
163         Dim _Handle As Action(Of T)
164
165         Public Event TaskJobComplete()
166
167         Sub New(Input As T, Handle As Action(Of T))
168             _Input = Input
169             _Handle = Handle
170         End Sub
171
172         Public Function Start() As Task(Of T)
173             _TaskComplete = False
174             _RunningTask = True
175             Call New Threading.Thread(AddressOf __invokeTask).Start()
176             Call Threading.Thread.Sleep(1)
177             Call New Threading.Thread(AddressOf RaisingEvent).Start()
178             Return Me
179         End Function
180
181         Private Sub RaisingEvent()
182             Call WaitForExit()
183             RaiseEvent TaskJobComplete()
184         End Sub
185
186         Protected Overrides Sub __invokeTask()
187             _TaskComplete = False
188             _RunningTask = True
189             Call _Handle(_Input)
190             _TaskComplete = True
191             _RunningTask = False
192         End Sub
193     End Class
194 End Namespace