-
Dec 23rd, 2016, 07:55 PM
#1
Typewriter Simulation
The following code prints a String written slowly between each Char found in a String to simulate the slow methodical typing of a typewriter.
The following code can be used in a Console Application:
Code:
''' <summary>
''' Prints a String slowly to the Console between each Char found in a String to simulate the slow methodical typing of a typewriter.
''' </summary>
''' <param name="text">The text to print.</param>
''' <remarks>The default pause between each Char is 100ms and the default pause between each word is 250ms.</remarks>
Private Sub Typewriter(ByVal text As String)
Dim words() As String = text.Split(" "c)
Dim t As Task = Task.Run(Sub()
For Each word As String In words
For Each letter As Char In word
Console.Write(letter)
Threading.Thread.Sleep(100)
Next
Console.Write(" ")
Threading.Thread.Sleep(250)
Next
End Sub)
t.Wait()
End Sub
''' <summary>
''' Prints a String slowly to the Console between each Char found in a String to simulate the slow methodical typing of a typewriter.
''' </summary>
''' <param name="text">The text to print.</param>
''' <param name="charPause">The amount of time, in milliseconds, to pause between printing each Char.</param>
''' <param name="wordPause">The amount of time, in milliseconds, to pause between printing each Word.</param>
Private Sub Typewriter(ByVal text As String, ByVal charPause As Integer, ByVal wordPause As Integer)
Dim words() As String = text.Split(" "c)
Dim t As Task = Task.Run(Sub()
For Each word As String In words
For Each letter As Char In word
Console.Write(letter)
Threading.Thread.Sleep(charPause)
Next
Console.Write(" ")
Threading.Thread.Sleep(wordPause)
Next
End Sub)
t.Wait()
End Sub
-
Dec 24th, 2016, 03:33 PM
#2
Addicted Member
Re: Typewriter Simulation
Because using Task class, that code requires .NET Framework 4.0 https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
What is the benefit of using Task class here?
-
Dec 24th, 2016, 11:25 PM
#3
Re: Typewriter Simulation
They're simpler to use than Threads, but for a detailed MSDN article check here: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
-
Dec 28th, 2016, 10:24 PM
#4
Re: Typewriter Simulation
I think the question was why use a task at all, since you are waiting on the task to complete, the call won't return so you're not doing parallel things.
Why not just have the sub run in the current thread?
Code:
Private Sub Typewriter(ByVal text As String)
Dim words() As String = text.Split(" "c)
For Each word As String In words
For Each letter As Char In word
Console.Write(letter)
Threading.Thread.Sleep(100)
Next
Console.Write(" ")
Threading.Thread.Sleep(250)
Next
End Sub
-
Dec 28th, 2016, 10:29 PM
#5
Re: Typewriter Simulation
I suppose it could be ran on the current thread in Console Applications, but my thought process was that someone could use this in a Windows Form Application with a few modifications and I wouldn't want it to tie up the UI thread.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|