# .NET and More > WPF, WCF, WF >  [RESOLVED] Thread, dispatcher - can't figure it out how to use it

## Salvadk

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:


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

----------


## szlamany

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

This is how I use the dispatcher



```
      this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate()
      {
          ThreadInfo.Content = "Reset and ready";
          searchBusy(false);
      });
```

This is C# - but you should get the idea.

----------


## Salvadk

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#

----------


## szlamany

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



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



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

----------


## Salvadk

Solution woring with Forms found her http://www.vbforums.com/showthread.p...-how-to-use-it

----------


## Salvadk

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

Howand where would you put the dispatcher here:


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

----------


## szlamany

System.Windows.Threading is wrong.

----------


## szlamany

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

----------


## Salvadk

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)  :Smilie:

----------


## Salvadk

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

----------

