Results 1 to 10 of 10

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 button. Right now I can only do it when the speak stops.

    Here is my code:
    Code:
    Imports System.Speech.Synthesis.SpeechSynthesizer
    Class MainWindow
        Dim sig As New Speech.Synthesis.SpeechSynthesizer()
        Dim rate As Double
        Dim StartTekst As String = "This is a test on how it sounds"
    
        Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles Window.Loaded
            txtboxSigTekst.Text = StartTekst
        End Sub
    
        Private Sub btnSigTekst_Click(sender As Object, e As RoutedEventArgs) Handles btnSigTekst.Click
            SigTekst(txtboxSigTekst.Text)
        End Sub
    
        Private Sub sliderRate_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double)) Handles sliderRate.ValueChanged
            rate = sliderRate.Value
            lblRate.Content = sliderRate.Value
        End Sub
    
        Private Sub SigTekst(SubSigTekst As String)
            sig.Rate = CInt(rate)
            sig.Speak(txtboxSigTekst.Text) ' SubSigTekst)
        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 do I do wrong? Do I insert Dispatcher in the sub SigTekst or should it be in another place?

    Can anyone help me to understand this?

    -Salva
    Last edited by Salvadk; Sep 25th, 2015 at 08:45 AM.

  2. #2
    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

    Putting [code][/code] tags around your code will make it easier to read!

    This is how I use the dispatcher

    Code:
          this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate()
          {
              ThreadInfo.Content = "Reset and ready";
              searchBusy(false);
          });
    This is C# - but you should get the idea.

    *** 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

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2015
    Posts
    11

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

    Thanks Szlamany for reminding me about the [code] tip.

    I have read and tried many variations of Dispatcher but with no luck. I'm beginning to suspect, that i'm using it in a wrong way. Thats why I have showed my code.
    Do you have any how to insert your example in my code. It's okay with c#

  4. #4
    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

    Ok - I see - you haven't even started a new thread yet - got it!

    Here is how I start a thread - I use a TaskFactory

    Code:
    private System.Threading.Tasks.TaskFactory taskFactory;
    .
    .
    .
    taskFactory = new System.Threading.Tasks.TaskFactory(); <-- in some form load like event...
    Then later on I start a task (thread) like this

    Code:
    System.Threading.Tasks.Task ta = taskFactory.StartNew(() => runST(fsOb, prefixes, returnMessage));
    runST is the function I'm going to run in this other thread.

    And of course I need to use the Dispatcher to talk back to the UI

    *** 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

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2015
    Posts
    11

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

    Solution woring with Forms found her http://www.vbforums.com/showthread.p...-how-to-use-it
    Last edited by Salvadk; Sep 27th, 2015 at 03:12 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2015
    Posts
    11

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

    I can't get it to work in WPF as you pointed out.

    Howand where would you put the dispatcher here:
    Code:
    Imports System.Speech.Synthesis.SpeechSynthesizer
    Imports System.Windows.Threading
    Class MainWindow
        Dim StartTekst As String = "This is a test on how it sounds when the speech synthesizer says what you typed in the text box"
        Dim sig As New Speech.Synthesis.SpeechSynthesizer()
        Dim MyDispatcher = Windows.Threading.Dispatcher.CurrentDispatcher
        Delegate Sub MyDelegate()
        Dim del As MyDelegate
    
        Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles Window.Loaded
            txtboxSigTekst.Text = StartTekst
        End Sub
    
        Private Sub btnSigTekst_Click(sender As Object, e As RoutedEventArgs) Handles btnSigTekst.Click
            del = New MyDelegate(AddressOf SigTekst)
            Dispatcher.BeginInvoke(New Action(AddressOf SigTekst))
        End Sub
    
        Private Sub SigTekst() 'SubSigTekst As String)
            sig.Speak(StartTekst)
        End Sub
    End Class
    The code runs but freeze the UI

    -salva

  7. #7
    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

    System.Windows.Threading is wrong.

    *** 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

  8. #8
    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

    Did you try the namespace and functions I showed you yet?

    *** 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

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2015
    Posts
    11

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

    Yes I did, although your syntax is a bit different in VB.
    You can try to copy/past the code in a VB project. You just have to make a reference to the speech dll. (search for speech)

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2015
    Posts
    11

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

    Ok I found out what the problem was. I have to use the property "SpeakAsync" to make the UI responsive and not just "Speak".

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