1 | #Region "Microsoft.VisualBasic::b61a73aad5446ea7dbfab3dd3f44ffac, Microsoft.VisualBasic.Core\ApplicationServices\Parallel\Tasks\CallbackTask.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 | ' Interface ICallbackTask |
35 | ' |
36 | ' Properties: CallbackInvoke |
37 | ' |
38 | ' Class CallbackTask |
39 | ' |
40 | ' Properties: Task |
41 | ' |
42 | ' Constructor: (+1 Overloads) Sub New |
43 | ' |
44 | ' Function: ToString |
45 | ' |
46 | ' Sub: Cancel, Start |
47 | ' |
48 | ' |
49 | ' /********************************************************************************/ |
50 | |
51 | #End Region |
52 | |
53 | Imports System.Threading |
54 | Imports Microsoft.VisualBasic.ComponentModel |
55 | Imports Microsoft.VisualBasic.Serialization |
56 | Imports Microsoft.VisualBasic.Serialization.JSON |
57 | |
58 | Namespace Parallel.Tasks |
59 | |
60 | Public Interface ICallbackTask |
61 | ReadOnly Property CallbackInvoke As Action |
62 | End Interface |
63 | |
64 | ''' <summary> |
65 | ''' When the task job complete, then the program will notify user code through callback function. |
66 | ''' </summary> |
67 | Public Class CallbackTask : Inherits ICallbackInvoke |
68 | |
69 | Public ReadOnly Property Task As Action |
70 | |
71 | Sub New(task As Action, callback As Action) |
72 | Call MyBase.New(callback) |
73 | Me.Task = task |
74 | End Sub |
75 | |
76 | Dim __running As Boolean = False |
77 | Dim __cts As New CancellationTokenSource |
78 | |
79 | Public Sub Cancel() |
80 | Call __cts.Cancel() |
81 | End Sub |
82 | |
83 | Public Sub Start() |
84 | #If NET_40 = 0 Then |
85 | If __running Then |
86 | Return |
87 | Else |
88 | __running = True |
89 | End If |
90 | |
91 | Call RunTask(Async Sub() Await __run(__cts)) |
92 | #End If |
93 | End Sub |
94 | |
95 | #If NET_40 = 0 Then |
96 | #Disable Warning |
97 | Private Async Function __run(cts As CancellationTokenSource) As Threading.Tasks.Task |
98 | #Enable Warning |
99 | Call Me._Task() |
100 | Call Me._execute() |
101 | __running = False |
102 | End Function |
103 | #End If |
104 | Public Overrides Function ToString() As String |
105 | Return New With { |
106 | .Task = Task.ToString, |
107 | .callback = CallbackInvoke.ToString, |
108 | .Running = __running.ToString |
109 | }.GetJson |
110 | End Function |
111 | End Class |
112 | End Namespace |