Amazon.com Widgets All posts tagged 'console'

WilliaBlog.Net

I dream in code

About the author

Robert Williams is an internet application developer for the Salem Web Network.
E-mail me Send mail
Code Project Associate Logo
Go Daddy Deal of the Week: 30% off your order at GoDaddy.com! Offer expires 11/6/12

Recent comments

Archive

Authors

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.


Traditional Console Spinner

Add this class to your console application, to assure the users that long running tasks are still working.

 

    1 Imports System.Threading

    2 

    3 Public Class ConsoleSpinner

    4     ' Methods

    5     Public Sub New(ByVal ProcessingMsg As String, ByVal FinishedMessage As String)

    6         Me.processingMessage = ProcessingMsg

    7         Me.finishedMessage = FinishedMessage

    8         Me.workerThread = New Thread(New ThreadStart(AddressOf Me.Spin))

    9         Me.workerThread.Priority = ThreadPriority.Lowest

   10         Me.workerThread.Name = "ConsoleSpinner"

   11     End Sub

   12 

   13     Private Sub Spin()

   14         Dim index As Integer = 0

   15         Do While (1 <> 0)

   16             Console.Write(ChrW(13) & "{0} {1}", Me.processingMessage, ConsoleSpinner.anim(index))

   17             index += 1

   18             If (index >= ConsoleSpinner.anim.Length) Then

   19                 index = 0

   20             End If

   21             Thread.Sleep(100)

   22         Loop

   23     End Sub

   24 

   25     Public Sub Start()

   26         Me.workerThread.Start()

   27     End Sub

   28 

   29     Public Sub [Stop]()

   30         Me.workerThread.Abort()

   31         Console.WriteLine("")

   32         Console.WriteLine(Me.finishedMessage)

   33     End Sub

   34 

   35 

   36     ' Fields

   37     Private Shared anim As String() = New String() {"|", "/", "-", "\", "|", "/", "-", "\"}

   38     Private finishedMessage As String

   39     Public processingMessage As String = String.Empty

   40     Private workerThread As Thread

   41 End Class

Call it from any point in your main program using:

   Dim cs As New ConsoleSpinner("Working...", "Done.")

   cs.Start()
  

   ' Do Something that may take a while...

 

   cs.Stop()

 


Categories: VB
Posted by Williarob on Saturday, December 13, 2008 7:10 AM
Permalink | Comments (0) | Post RSSRSS comment feed