| 1 | #Region "Microsoft.VisualBasic::8ebf39e8b7a77bb3f9a58d9bb322f5e6, Microsoft.VisualBasic.Core\ApplicationServices\Parallel\OperationTimeOut.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 |     '     Module TimeOutAPI | 
| 35 |     '  | 
| 36 |     '         Function: (+3 Overloads) OperationTimeOut | 
| 37 |     '  | 
| 38 |     '  | 
| 39 |     ' /********************************************************************************/ | 
| 40 | |
| 41 | #End Region | 
| 42 | |
| 43 | Imports System.Runtime.CompilerServices | 
| 44 | Imports Microsoft.VisualBasic.Parallel.Tasks | 
| 45 | |
| 46 | Namespace Parallel | 
| 47 | |
| 48 |     Public Module TimeOutAPI | 
| 49 | |
| 50 |         ''' <summary> | 
| 51 |         ''' The returns value of TRUE represent of the target operation has been time out.(返回真,表示操作超时) | 
| 52 |         ''' </summary> | 
| 53 |         ''' <typeparam name="T"></typeparam> | 
| 54 |         ''' <typeparam name="TOut"></typeparam> | 
| 55 |         ''' <param name="handle"></param> | 
| 56 |         ''' <param name="Out"></param> | 
| 57 |         ''' <param name="TimeOut">The time unit of this parameter is second.(单位为秒)</param> | 
| 58 |         ''' <returns></returns> | 
| 59 |         ''' <remarks></remarks> | 
| 60 |         '''  | 
| 61 |         <Extension> | 
| 62 |         Public Function OperationTimeOut(Of T, TOut)(handle As Func(Of T, TOut), [In] As T, ByRef Out As TOut, TimeOut As Double) As Boolean | 
| 63 |             Dim invoke As New __backgroundTask(Of TOut)(Function() handle([In])) | 
| 64 |             Dim i As Integer | 
| 65 | |
| 66 |             TimeOut = TimeOut * 1000 | 
| 67 |             invoke.Start() | 
| 68 | |
| 69 |             Do While i < TimeOut | 
| 70 |                 If invoke.TaskComplete Then | 
| 71 |                     Out = invoke.Value | 
| 72 |                     Return False | 
| 73 |                 End If | 
| 74 | |
| 75 |                 i += 1 | 
| 76 |                 Call Threading.Thread.Sleep(1) | 
| 77 |             Loop | 
| 78 | |
| 79 |             Call invoke.Abort() | 
| 80 | |
| 81 |             Return True | 
| 82 |         End Function | 
| 83 | |
| 84 |         ''' <summary> | 
| 85 |         '''  | 
| 86 |         ''' </summary> | 
| 87 |         ''' <typeparam name="T"></typeparam> | 
| 88 |         ''' <param name="handle"></param> | 
| 89 |         ''' <param name="Out"></param> | 
| 90 |         ''' <param name="TimeOut">The time unit of this parameter is second.(单位为秒)</param> | 
| 91 |         ''' <returns></returns> | 
| 92 |         '''  | 
| 93 |         <Extension> | 
| 94 |         Public Function OperationTimeOut(Of T)(handle As Func(Of T), ByRef Out As T, TimeOut As Double) As Boolean | 
| 95 |             Return OperationTimeOut(Of Boolean, T)(Function(b) handle(), True, Out, TimeOut) | 
| 96 |         End Function | 
| 97 | |
| 98 |         ''' <summary> | 
| 99 |         '''  | 
| 100 |         ''' </summary> | 
| 101 |         ''' <param name="handle"></param> | 
| 102 |         ''' <param name="TimeOut">The time unit of this parameter is second.(单位为秒)</param> | 
| 103 |         ''' <returns></returns> | 
| 104 |         '''  | 
| 105 |         <Extension> | 
| 106 |         Public Function OperationTimeOut(handle As Action, TimeOut As Double) As Boolean | 
| 107 |             Dim invoke As Func(Of Boolean, Boolean) = | 
| 108 |                 Function(b) As Boolean | 
| 109 |                     Call handle() | 
| 110 |                     Return True | 
| 111 |                 End Function | 
| 112 |             Return OperationTimeOut(invoke, True, True, TimeOut) | 
| 113 |         End Function | 
| 114 |     End Module | 
| 115 | End Namespace |