|
-
Sep 25th, 2015, 06:17 AM
#1
Thread Starter
New Member
[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.
-
Sep 25th, 2015, 08:34 AM
#2
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.
-
Sep 25th, 2015, 08:50 AM
#3
Thread Starter
New Member
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#
-
Sep 25th, 2015, 08:59 AM
#4
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
-
Sep 26th, 2015, 04:05 PM
#5
Thread Starter
New Member
Re: Thread, dispatcher - can't figure it out how to use it
Last edited by Salvadk; Sep 27th, 2015 at 03:12 PM.
-
Sep 27th, 2015, 05:23 PM
#6
Thread Starter
New Member
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
-
Sep 27th, 2015, 05:53 PM
#7
Re: Thread, dispatcher - can't figure it out how to use it
System.Windows.Threading is wrong.
-
Sep 27th, 2015, 06:36 PM
#8
Re: Thread, dispatcher - can't figure it out how to use it
Did you try the namespace and functions I showed you yet?
-
Sep 28th, 2015, 01:42 AM
#9
Thread Starter
New Member
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)
-
Sep 28th, 2015, 04:33 AM
#10
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|