microsoft-visualbasic-runtime/ApplicationServices/Parallel/Tasks/Task.vb

140 lines
4.2 KiB
VB.net
Raw Normal View History

2020-03-28 15:44:37 +08:00
#Region "Microsoft.VisualBasic::d42e865588a0ba13d09d6f8fbd4c03c9, Microsoft.VisualBasic.Core\ApplicationServices\Parallel\Tasks\Task.vb"
2018-08-02 20:14:48 +08:00
2020-03-28 15:44:37 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
2020-03-19 20:39:53 +08:00
2020-03-28 15:44:37 +08:00
' /********************************************************************************/
2020-03-19 20:39:53 +08:00
2020-03-28 15:44:37 +08:00
' Summaries:
2020-03-19 20:39:53 +08:00
2020-03-28 15:44:37 +08:00
' Class Task
'
' Properties: Value
'
' Constructor: (+1 Overloads) Sub New
'
' Function: GetValue, Start
'
' Sub: doInvokeTask
'
' Class Task
'
' Constructor: (+1 Overloads) Sub New
'
' Function: Start
'
' Sub: doInvokeTask
'
'
' /********************************************************************************/
2018-08-02 20:14:48 +08:00
#End Region
Namespace Parallel.Tasks
''' <summary>
''' 更加底层的线程模式和LINQ相比不会受到CPU核心数目的限制
''' </summary>
''' <typeparam name="T">后台任务的执行参数</typeparam>
''' <typeparam name="TOut">后台任务的执行结果</typeparam>
Public Class Task(Of T, TOut) : Inherits IParallelTask
Dim _Handle As Func(Of T, TOut)
Dim _Input As T
''' <summary>
2020-03-19 20:39:53 +08:00
''' 假若任务已经完成,则会返回计算值,假若没有完成,则只会返回空值,
''' 假若想要在任何情况之下都会得到后台任务所执行的计算结果,
''' 请使用<see cref="GetValue()"/>方法
2018-08-02 20:14:48 +08:00
''' </summary>
''' <returns></returns>
Public ReadOnly Property Value As TOut
''' <summary>
2020-03-19 20:39:53 +08:00
''' 假若后台任务还没有完成,则函数会一直阻塞在这里直到任务执行完毕,
''' 假若任务早已完成,则函数会立即返回数据
2018-08-02 20:14:48 +08:00
''' </summary>
''' <returns></returns>
Public Function GetValue() As TOut
If Not Me.TaskRunning Then
Call Start()
End If
Call WaitForExit()
2020-03-19 20:39:53 +08:00
Return Value
2018-08-02 20:14:48 +08:00
End Function
Sub New(Input As T, Handle As Func(Of T, TOut))
_Handle = Handle
_Input = Input
End Sub
Public Function Start() As Task(Of T, TOut)
2020-03-19 20:39:53 +08:00
TaskComplete = False
2018-08-02 20:14:48 +08:00
_RunningTask = True
2020-03-19 20:39:53 +08:00
Call RunTask(AddressOf doInvokeTask)
2018-08-02 20:14:48 +08:00
Return Me
End Function
2020-03-19 20:39:53 +08:00
Protected Overrides Sub doInvokeTask()
TaskComplete = False
2018-08-02 20:14:48 +08:00
_RunningTask = True
_Value = _Handle(_Input)
2020-03-19 20:39:53 +08:00
TaskComplete = True
2018-08-02 20:14:48 +08:00
_RunningTask = False
End Sub
End Class
Public Class Task(Of T) : Inherits IParallelTask
2020-03-19 20:39:53 +08:00
Dim inputVal As T
Dim handleTask As Action(Of T)
2018-08-02 20:14:48 +08:00
Public Event TaskJobComplete()
Sub New(Input As T, Handle As Action(Of T))
2020-03-19 20:39:53 +08:00
inputVal = Input
handleTask = Handle
2018-08-02 20:14:48 +08:00
End Sub
Public Function Start() As Task(Of T)
2020-03-19 20:39:53 +08:00
Call RunTask(AddressOf doInvokeTask)
2018-08-02 20:14:48 +08:00
Return Me
End Function
2020-03-19 20:39:53 +08:00
Protected Overrides Sub doInvokeTask()
TaskComplete = False
2018-08-02 20:14:48 +08:00
_RunningTask = True
2020-03-19 20:39:53 +08:00
Call handleTask(inputVal)
TaskComplete = True
2018-08-02 20:14:48 +08:00
_RunningTask = False
2020-03-19 20:39:53 +08:00
RaiseEvent TaskJobComplete()
2018-08-02 20:14:48 +08:00
End Sub
End Class
End Namespace