Results 1 to 19 of 19

Thread: Runing something in a different thread

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    23

    Runing something in a different thread

    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

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Runing something in a different thread

    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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    23

    Re: Runing something in a different thread

    hey um thanks for that but i am having a bit difficulties initiating it through the Thread class

  4. #4
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Runing something in a different thread

    Quote Originally Posted by Th3man View Post
    hey um thanks for that but i am having a bit difficulties initiating it through the Thread class
    You have to help me help you. If it's not working, I need to know what's happening, what you're trying, any errors that occur, etc.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    23

    Re: Runing something in a different thread

    well okay im making a test program where

    button 1 code:
    vb Code:
    1. Dim SAPI
    2. SAPI = CreateObject("sapi.spvoice")
    3. SAPI.Speak(TextBox1.Text)

    but when its reading the text i cant click my other button

    button 2 code:
    vb Code:
    1. Dim SAPI
    2.  SAPI = CreateObject("sapi.spvoice")
    3.  SAPI.pause

    so i want to make button 1 run on a different thread and i was wondering how to do that

  6. #6
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Runing something in a different thread

    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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    23

    Re: Runing something in a different thread

    The method was

    speak:

    vb Code:
    1. Dim SAPI
    2. SAPI = CreateObject("sapi.spvoice")
    3. SAPI.Speak(TextBox1.Text)

    Thats how far i went i kkinda got confused after that,a nd i di read the page

  8. #8
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Runing something in a different thread

    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:
    1. Imports System.Speech.Synthesis
    2.  
    3. Dim speaker as New SpeechSynthesizer()
    4.  
    5. speaker.Rate = 1
    6. speaker.Volume = 100
    7. speaker.Speak(TextBox1.Text)
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  9. #9
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Runing something in a different thread

    Quote Originally Posted by Th3man View Post
    The method was

    speak:

    vb Code:
    1. Dim SAPI
    2. SAPI = CreateObject("sapi.spvoice")
    3. SAPI.Speak(TextBox1.Text)

    Thats how far i went i kkinda got confused after that,a nd i di read the page
    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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  10. #10
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Runing something in a different thread

    Also, I should point out that you can use a BackgroundWorker to do this, but I personally hate using that method.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    23

    Re: Runing something in a different thread

    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


  12. #12
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Runing something in a different thread

    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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    23

    Re: Runing something in a different thread

    Where would i ask threading to start?

  14. #14
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Runing something in a different thread

    Quote Originally Posted by Th3man View Post
    Where would i ask threading to start?
    By calling start on the thread variable you created. Please for the love of Bob, look at the example on the MSDN site I linked.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    23

    Re: Runing something in a different thread

    I read it and still cant get it may you please just show me an example just this one time

  16. #16
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Runing something in a different thread

    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.

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    23

    Re: Runing something in a different thread

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

  18. #18

  19. #19
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: Runing something in a different thread

    @TH3man

    You need to start a thread after you specify it

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click
    2.  
    3. Dim t as Thread
    4. t = New Thread(AddressOf speak)
    5. t.Start
    6. End Sub
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

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