| 1 | #Region "Microsoft.VisualBasic::f29f541c7a4f3f5cdc71b0538e143f6f, Microsoft.VisualBasic.Core\ApplicationServices\Terminal\Utility\CBusyIndicator.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 CBusyIndicator |
| 35 | ' |
| 36 | ' Constructor: (+1 Overloads) Sub New |
| 37 | ' Sub: [Stop], (+2 Overloads) Dispose, DoEvents, Start |
| 38 | ' |
| 39 | ' |
| 40 | ' /********************************************************************************/ |
| 41 | |
| 42 | #End Region |
| 43 | |
| 44 | Imports System.Runtime.CompilerServices |
| 45 | Imports System.Threading |
| 46 | Imports Microsoft.VisualBasic.Parallel |
| 47 | |
| 48 | Namespace Terminal.Utility |
| 49 | |
| 50 | ''' <summary> |
| 51 | ''' The console BusyIndicator |
| 52 | ''' </summary> |
| 53 | Public Class CBusyIndicator : Implements IDisposable |
| 54 | |
| 55 | Dim _indicatorStyle As Char |
| 56 | Dim _OnRunningState As Boolean = False |
| 57 | Dim _TicksCount As Integer |
| 58 | |
| 59 | Sub New(Optional IndicatorStyle As Char = "."c, Optional _start As Boolean = False, Optional Ticks As Integer = -1) |
| 60 | _indicatorStyle = IndicatorStyle |
| 61 | If _start Then Call Start(Ticks) |
| 62 | End Sub |
| 63 | |
| 64 | Private Sub DoEvents() |
| 65 | Do While _OnRunningState = True |
| 66 | |
| 67 | Call Thread.Sleep(1000) |
| 68 | Call STDIO.Write(_indicatorStyle) |
| 69 | |
| 70 | If _TicksCount > 0 Then |
| 71 | _TicksCount -= 1 |
| 72 | Else |
| 73 | If _TicksCount <> -1 Then |
| 74 | _OnRunningState = False |
| 75 | End If |
| 76 | End If |
| 77 | Loop |
| 78 | End Sub |
| 79 | |
| 80 | ''' <summary> |
| 81 | ''' 运行进度条 |
| 82 | ''' </summary> |
| 83 | ''' <param name="Ticks">The total ticking counts of the indicator, Unit is [second].</param> |
| 84 | ''' <remarks></remarks> |
| 85 | Public Sub Start(Optional Ticks As Integer = -1) |
| 86 | If _OnRunningState = True Then |
| 87 | Return |
| 88 | End If |
| 89 | |
| 90 | _TicksCount = Ticks |
| 91 | _OnRunningState = True |
| 92 | |
| 93 | Call RunTask(AddressOf DoEvents) |
| 94 | End Sub |
| 95 | |
| 96 | Public Sub [Stop]() |
| 97 | _OnRunningState = False |
| 98 | End Sub |
| 99 | |
| 100 | #Region "IDisposable Support" |
| 101 | Private disposedValue As Boolean ' To detect redundant calls |
| 102 | |
| 103 | ' IDisposable |
| 104 | Protected Overridable Sub Dispose(disposing As Boolean) |
| 105 | If Not Me.disposedValue Then |
| 106 | If disposing Then |
| 107 | ' TODO: dispose managed state (managed objects). |
| 108 | _OnRunningState = False |
| 109 | End If |
| 110 | |
| 111 | ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. |
| 112 | ' TODO: set large fields to null. |
| 113 | End If |
| 114 | Me.disposedValue = True |
| 115 | End Sub |
| 116 | |
| 117 | ' TODO: override Finalize() only if Dispose( disposing As Boolean) above has code to free unmanaged resources. |
| 118 | 'Protected Overrides Sub Finalize() |
| 119 | ' ' Do not change this code. Put cleanup code in Dispose( disposing As Boolean) above. |
| 120 | ' Dispose(False) |
| 121 | ' MyBase.Finalize() |
| 122 | 'End Sub |
| 123 | |
| 124 | ' This code added by Visual Basic to correctly implement the disposable pattern. |
| 125 | Public Sub Dispose() Implements IDisposable.Dispose |
| 126 | ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. |
| 127 | Dispose(True) |
| 128 | GC.SuppressFinalize(Me) |
| 129 | End Sub |
| 130 | #End Region |
| 131 | |
| 132 | End Class |
| 133 | End Namespace |