Hey guys,
Sorry to bother you again but this time i was wondering how i can run whatever button_1 does in a different thread
Thanks in advance
Printable View
Hey guys,
Sorry to bother you again but this time i was wondering how i can run whatever button_1 does in a different thread
Thanks in advance
Create a method, like a sub or function, process your code in it, and initiate it through the Thread class. You can view the documentation on MSDN here.
hey um thanks for that but i am having a bit difficulties initiating it through the Thread class
well okay im making a test program where
button 1 code:
vb Code:
Dim SAPI SAPI = CreateObject("sapi.spvoice") SAPI.Speak(TextBox1.Text)
but when its reading the text i cant click my other button
button 2 code:
vb Code:
Dim SAPI SAPI = CreateObject("sapi.spvoice") SAPI.pause
so i want to make button 1 run on a different thread and i was wondering how to do that
That's the code that should be in the method. Where's the method you've created and the code for the thread?
That code should be in your method and thread code should be in the button. Did you even read the MSDN page? It even gives an example at the bottom.
The method was
speak:
vb Code:
Dim SAPI SAPI = CreateObject("sapi.spvoice") SAPI.Speak(TextBox1.Text)
Thats how far i went i kkinda got confused after that,a nd i di read the page :o
And that code looks like VB6 code. You shouldn't be using that. You should be using the VB.NET code. Also, you need to turn on Option Strict. It will help prevent errors. If it were on, it would let you create an object without specifying its type.
VB.NET Code:
Imports System.Speech.Synthesis Dim speaker as New SpeechSynthesizer() speaker.Rate = 1 speaker.Volume = 100 speaker.Speak(TextBox1.Text)
That's not a method. I already told you what a method is. It's a sub or function. In this case, you just want to create a sub, place the code in the sub and then call the sub through the thread via AddressOf. Just read the page. If you're confused, then ask what whatever you're confused about means and I'll answer it. But, like I said, it gives you an example at the bottom of the page.
I'm not going to give you the answer. You have to put in the work and igure it out. I'm just leading you to it.
Also, I should point out that you can use a BackgroundWorker to do this, but I personally hate using that method.
so in button1 i would initiate the thread class ?
so the code would be
Option Explicit ON
Imports System
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click
Dim t As New Thread(AddressOf speak)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button2.Click
Dim SAPI
SAPI = CreateObject("sapi.spvoice")
sapi.pause()
End Sub
Private Sub speak
Dim SAPI
SAPI = CreateObject("sapi.spvoice")
SAPI.Speak(TextBox1.Text)
t.sleep()
End Sub
End Class
Thats how far i am
So, other than the old and outdated code, you're almost there.
Basically, you've lined up your racers, but you haven't told them to Start.
Where would i ask threading to start?
I read it and still cant get it may you please just show me an example just this one time
you won't get far in your programming career without learning how to get around the msdn documentation... it's right there where he linked: http://msdn.microsoft.com/en-us/library/6x4c42hc.aspx
shows exactly how to start a thread you just declared. he even told you NOT to use VB6 code, yet post #11 shows you threw that suggestion to the wind.
I learn from examples and if you guys are just gonna not help the way that helps me the most is okay. Im not trying to disrepect but yea right now...
well there's an example on the msdn page.
@TH3man
You need to start a thread after you specify it
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click Dim t as Thread t = New Thread(AddressOf speak) t.Start End Sub