Results 1 to 5 of 5

Thread: Typewriter Simulation

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Addicted Member
    Join Date
    Dec 2011
    Posts
    194

    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?
    On Error GoTo Hell

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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

  5. #5

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width