Results 1 to 8 of 8

Thread: [RESOLVED] Thread, dispatcher - can't figure it out how to use it

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2015
    Posts
    11

    Resolved [RESOLVED] Thread, dispatcher - can't figure it out how to use it

    Hi all

    I'm learning how to start a new thread so my functions and GUI not freezing, but I don't understand how to use it.

    I have made a small program that says what I put into the textbox, and while it's speaking, I want to be able to slide the slide bar or move the GUI around. Right now I can only do it when the speak stops.

    Here is my code:
    Code:
    Imports System.Speech.Synthesis.SpeechSynthesizer
    Public Class Form1
        Dim sig As New Speech.Synthesis.SpeechSynthesizer()
        Dim rate As Double
        Dim StartTekst As String = "This is a test on how it sounds when the speech synthesizer says"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            txtboxSigTekst.Text = StartTekst
        End Sub
        Private Sub btnSigTekst_Click(sender As Object, e As EventArgs) Handles btnSigTekst.Click
            SigTekst()
        End Sub
        Private Sub SigTekst()
            sig.Speak(txtboxSigTekst.Text)
        End Sub
    End Class
    I have tried to insert a "dispatch" in the sub SigTekst (Dispatcher.BeginInvoke(New Action(AddressOf SigTekst))) but non of the different metodes I have tried, is working.
    What am I doing wrong? Do I insert Dispatcher in the sub SigTekst or should it be in another place?

    Can anyone help me to understand this? I think I better understand this if you could insert the code, as simple as possible, to start the sub in ad new thread.

    -Salva

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Thread, dispatcher - can't figure it out how to use it

    try this:

    Code:
    Imports System.Speech.Synthesis.SpeechSynthesizer
    
    Public Class Form1
    
        Dim sig As New Speech.Synthesis.SpeechSynthesizer()
        Dim rate As Double
        Dim StartTekst As String = "This is a test on how it sounds when the speech synthesizer says"
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            txtboxSigTekst.Text = StartTekst
        End Sub
    
        Private Sub btnSigTekst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSigTekst.Click
            Dim t As New Threading.Thread(AddressOf SigTekst)
            t.Start(txtboxSigTekst.Text)
        End Sub
    
        Private Sub SigTekst(ByVal sayWhat As Object)
            sig.Speak(sayWhat.ToString)
        End Sub
    
    End Class

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Thread, dispatcher - can't figure it out how to use it

    Why do you have two threads going on this topic - that stinks!

    http://www.vbforums.com/showthread.p...-how-to-use-it

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Thread, dispatcher - can't figure it out how to use it

    Quote Originally Posted by szlamany View Post
    Why do you have two threads going on this topic - that stinks!

    http://www.vbforums.com/showthread.p...-how-to-use-it
    I prefer my solution

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2015
    Posts
    11

    Re: Thread, dispatcher - can't figure it out how to use it

    The other one is WPF code and not many people is active in WPF.
    That why I created a form project

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2015
    Posts
    11

    Re: Thread, dispatcher - can't figure it out how to use it

    YES God damed! Its working. finally. Spend two days on this
    Just had to remove the parameter in t.Start()

    Thank you

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] Thread, dispatcher - can't figure it out how to use it

    Not passing the parameter that way causes cross thread calls that will affect your UI thread...

    Code:
    Private Sub btnSigTekst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSigTekst.Click
        Dim t As New Threading.Thread(AddressOf SigTekst)
        t.Start(txtboxSigTekst.Text) 'this is the parameter that is passed to SigTekst
    End Sub
    Code:
    Private Sub SigTekst(ByVal sayWhat As Object) 'this is the parameter that is sent in t.Start
        sig.Speak(sayWhat.ToString)
    End Sub

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] Thread, dispatcher - can't figure it out how to use it

    The TIMER in a regular WINFORM project is not the same as a WPF timer - which is the code I showed you - from a WPF program I have.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Tags for this 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
  •  



Click Here to Expand Forum to Full Width