To start a thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?anVhbg==?=

    To start a thread

    How can I do to start a thread. I tried everything... but it had no work.
    Any solution?
    It is a Sub working with a Timer control.
    I want to do two tasks at the same time (more or less for a minute).
    Visual Basic 2005.
    Thanks.

  • kimiraikkonen

    #2
    Re: To start a thread

    On May 15, 12:41 pm, juan <j...@discussio ns.microsoft.co mwrote:
    How can I do to start a thread. I tried everything... but it had no work.
    Any solution?
    It is a Sub working with a Timer control.
    I want to do two tasks at the same time (more or less for a minute).
    Visual Basic 2005.
    Thanks.
    Hi,
    This could be a basic example to run a seperate thread:

    Imports System.threadin g
    Module Module1

    Sub Main()
    Dim mythread As New Thread(AddressO f threadrun)
    mythread.Start( )
    End Sub

    Sub threadrun()
    For x As Integer = 0 To 10
    Console.WriteLi ne("this is a thread!")
    Next
    Console.ReadLin e()
    End Sub

    End Module


    Also check out MSDN for more thread class methods / properties:

    msdn.microsoft. com/en-us/library/ms951089.aspx


    Thanks,

    Onur Güzel

    Comment

    • Armin Zingler

      #3
      Re: To start a thread

      "juan" <juan@discussio ns.microsoft.co mschrieb
      How can I do to start a thread. I tried everything... but it had no
      work. Any solution?
      It is a Sub working with a Timer control.
      I want to do two tasks at the same time (more or less for a minute).
      Visual Basic 2005.
      Thanks.
      Which of the many links handling this topic do you want us to post? ;-)
      Have you already had a look in the threading topics in the VB and
      Framework docs? If you have a specific problem, which is it?

      (sry, can't send a link because the online MSDN lib TOC is all Italian
      now!??!)


      Armin

      Comment

      • Patrice

        #4
        Re: To start a thread

        What have you tried ? What doesn't work ? I's no sue for us to give you some
        code you already tried.

        As starting itself is quite simple I believe this is something more subtle
        that just starting the thread (you have a problem when updating the UI , do
        you ?).

        Depending on what you want to do you may want also to check the
        BackgroundWorke r class...

        --
        Patrice

        "juan" <juan@discussio ns.microsoft.co ma écrit dans le message de groupe de
        discussion : FE328E68-67B3-42B7-83A3-3A9DE174A4FB@mi crosoft.com...
        How can I do to start a thread. I tried everything... but it had no work.
        Any solution?
        It is a Sub working with a Timer control.
        I want to do two tasks at the same time (more or less for a minute).
        Visual Basic 2005.
        Thanks.
        >

        Comment

        • =?Utf-8?B?anVhbg==?=

          #5
          RE: To start a thread

          I think that the problem is that I need to associate one element (for play
          music) to the thread ("t"), and I don't know how.
          I have tried to give to "t.start()" an argument, but Visual Basic refuse it.
          It is an error.
          Any other solution?
          Thanks.

          "juan" wrote:
          How can I do to start a thread. I tried everything... but it had no work.
          Any solution?
          It is a Sub working with a Timer control.
          I want to do two tasks at the same time (more or less for a minute).
          Visual Basic 2005.
          Thanks.
          >

          Comment

          • Miro

            #6
            Re: To start a thread

            You technically can use a timer.

            Start the timer enabled = false
            set the interval to a second

            the first line of the timer put timer.enabled = false

            and the timer will run ' turn itself off ' but the sub finishes running.

            What are you trying to do in the other thread?

            Its a very dirty way of doing it, but it does do the trick.
            I did that once, as i dont fully understand how to do threads on my own yet,
            and ill come back and learn them as they come.

            Miro

            "juan" <juan@discussio ns.microsoft.co mwrote in message
            news:FE328E68-67B3-42B7-83A3-3A9DE174A4FB@mi crosoft.com...
            How can I do to start a thread. I tried everything... but it had no work.
            Any solution?
            It is a Sub working with a Timer control.
            I want to do two tasks at the same time (more or less for a minute).
            Visual Basic 2005.
            Thanks.
            >

            Comment

            • Patrice

              #7
              Re: To start a thread

              And you play the sound file using which method ? What kind of file is this
              (MIDI, WAV ?) In most cases it should be handled for you as playing a sound
              asynchronously is what you want most of the time...

              Try for example :


              Note that the AudioPlayMode.B ackground option ("Plays the sound in the
              background. The calling code continues to execute.").

              It's best to always provide an description of what you are trying to do
              rather than just how (in case someone would have another better/simpler way
              to do the same thing).

              --
              Patrice

              "juan" <juan@discussio ns.microsoft.co ma écrit dans le message de groupe de
              discussion : F0E72DF9-5312-4B40-BE28-811D1AA906D2@mi crosoft.com...
              I think that the problem is that I need to associate one element (for play
              music) to the thread ("t"), and I don't know how.
              I have tried to give to "t.start()" an argument, but Visual Basic refuse
              it.
              It is an error.
              Any other solution?
              Thanks.
              >
              "juan" wrote:
              >
              >How can I do to start a thread. I tried everything... but it had no work.
              >Any solution?
              >It is a Sub working with a Timer control.
              >I want to do two tasks at the same time (more or less for a minute).
              >Visual Basic 2005.
              >Thanks.
              >>

              Comment

              • rowe_newsgroups

                #8
                Re: To start a thread

                On May 15, 10:29 am, juan <j...@discussio ns.microsoft.co mwrote:
                I think that the problem is that I need to associate one element (for play
                music) to the thread ("t"), and I don't know how.
                I have tried to give to "t.start()" an argument, but Visual Basic refuse it.
                It is an error.
                Any other solution?
                Thanks.
                >
                "juan" wrote:
                How can I do to start a thread. I tried everything... but it had no work.
                Any solution?
                It is a Sub working with a Timer control.
                I want to do two tasks at the same time (more or less for a minute).
                Visual Basic 2005.
                Thanks.
                First off, google about sending parameters to new threads, as of 2.0
                you can do this without much trouble once you get used to the call
                structure.

                The other thing you could look at, which is now my personal favorite
                for managing threads, is to create a delegate that I will use to
                invoke a method asynchronously. I won't explain it here, you're sure
                to find plenty of articles on it on the web which will explain it much
                better than I would.

                Thanks,

                Seth Rowe [MVP]

                Comment

                • =?Utf-8?B?anVhbg==?=

                  #9
                  RE: To start a thread

                  Now I think that I have always had it. However the thread that plays music
                  seems to interrupt by the principal thread (the program thread). It is a
                  program for text processing.
                  Thanks in any case.

                  "juan" wrote:
                  I think that the problem is that I need to associate one element (for play
                  music) to the thread ("t"), and I don't know how.
                  I have tried to give to "t.start()" an argument, but Visual Basic refuse it.
                  It is an error.
                  Any other solution?
                  Thanks.
                  >
                  "juan" wrote:
                  >
                  How can I do to start a thread. I tried everything... but it had no work.
                  Any solution?
                  It is a Sub working with a Timer control.
                  I want to do two tasks at the same time (more or less for a minute).
                  Visual Basic 2005.
                  Thanks.

                  Comment

                  • Ben white

                    #10
                    Re: To start a thread

                    On May 16, 5:07 am, juan <j...@discussio ns.microsoft.co mwrote:
                    Now I think that I have always had it. However the thread that plays music
                    seems to interrupt by the principal thread (the program thread). It is a
                    program for text processing.
                    Thanks in any case.
                    >
                    "juan" wrote:
                    I think that the problem is that I need to associate one element (for play
                    music) to the thread ("t"), and I don't know how.
                    I have tried to give to "t.start()" an argument, but Visual Basic refuse it.
                    It is an error.
                    Any other solution?
                    Thanks.
                    >
                    "juan" wrote:
                    >
                    How can I do to start a thread. I tried everything... but it had no work.
                    Any solution?
                    It is a Sub working with a Timer control.
                    I want to do two tasks at the same time (more or less for a minute).
                    Visual Basic 2005.
                    Thanks.
                    I have had very good luck with the following methods:
                    1) using a timer to start the action, it is messy but it does work.
                    Make sure you put the minimum amount of work in the timer process. I
                    have used timers to fire delegates so that a longer starting process
                    doesn't conflict with the timer.
                    2) If at all possible, use a discreet object for your separate thread.
                    3) To manage synchronization and scope, I have often passed a
                    reference to the controlling (dispatching) thread to the object that
                    will operate separately.
                    4) For managed communication you use delegates to raise events from
                    the child (free threaded) object which lets the free thread
                    communicate with the sender.
                    if you feel more brave:
                    5) It is very instructional to look at thread pooling (I use it for an
                    application I have writes application files for Progress and then
                    launches them maintaining 10-15 running instances at a time.) I
                    mention this as you indicate doing background processing.
                    6) Thread callbacks, even if you choose not to use them, are worth
                    understanding if you are going to adventure into the maddening world
                    of threading.

                    You may want to check out an erarlier post I had at

                    for some code and links to MSDN explanations.

                    Hope this helps.
                    Viva la VB

                    Comment

                    Working...