How can I check for thread existense ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fniles

    How can I check for thread existense ?

    I am using VB.NET 2005. To check if the same program already run or not, I
    can use the following

    If Process.GetProc essesByName(Pro cess.GetCurrent Process.Process Name).Length
    1 Then
    MsgBox("Another instance of " & Process.GetCurr entProcess.Proc essName &
    " is already running !")
    End
    End If

    I am wondering if I can check the same thing for a thread.
    I have 1 thread that I do not want to create multiple instances of. Can I
    check whether the thread has exists before or not ?
    Thank you.


  • Spam Catcher

    #2
    Re: How can I check for thread existense ?

    "fniles" <fniles@pfmail. comwrote in
    news:#N7IxjN6HH A.600@TK2MSFTNG P05.phx.gbl:
    I am wondering if I can check the same thing for a thread.
    I have 1 thread that I do not want to create multiple instances of.
    Can I check whether the thread has exists before or not ?
    Thank you.
    You would use a System Mutex to check if an instance is running. There
    should be an example on google (used to be plenty, but I can't seem to find
    one!!!)

    But VB.NET 2005 already has an option to limit the application to one
    instance...

    Comment

    • Michel Posseth  [MCP]

      #3
      Re: How can I check for thread existense ?

      Mutexes are normally used for detecting multiple instances of the same app

      For threading you normally use the semaphore type

      regards

      michel


      "Spam Catcher" <spamhoneypot@r ogers.comschree f in bericht
      news:Xns99999EB 7D394Fusenethon eypotrogers@127 .0.0.1...
      "fniles" <fniles@pfmail. comwrote in
      news:#N7IxjN6HH A.600@TK2MSFTNG P05.phx.gbl:
      >
      >I am wondering if I can check the same thing for a thread.
      >I have 1 thread that I do not want to create multiple instances of.
      >Can I check whether the thread has exists before or not ?
      >Thank you.
      >
      You would use a System Mutex to check if an instance is running. There
      should be an example on google (used to be plenty, but I can't seem to
      find
      one!!!)
      >
      But VB.NET 2005 already has an option to limit the application to one
      instance...
      >

      Comment

      • fniles

        #4
        Re: How can I check for thread existense ?

        Thank you.
        What is semaphore type ?


        "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
        news:u6FuuZO6HH A.2380@TK2MSFTN GP02.phx.gbl...
        Mutexes are normally used for detecting multiple instances of the same app
        >
        For threading you normally use the semaphore type
        >
        regards
        >
        michel
        >
        >
        "Spam Catcher" <spamhoneypot@r ogers.comschree f in bericht
        news:Xns99999EB 7D394Fusenethon eypotrogers@127 .0.0.1...
        >"fniles" <fniles@pfmail. comwrote in
        >news:#N7IxjN6H HA.600@TK2MSFTN GP05.phx.gbl:
        >>
        >>I am wondering if I can check the same thing for a thread.
        >>I have 1 thread that I do not want to create multiple instances of.
        >>Can I check whether the thread has exists before or not ?
        >>Thank you.
        >>
        >You would use a System Mutex to check if an instance is running. There
        >should be an example on google (used to be plenty, but I can't seem to
        >find
        >one!!!)
        >>
        >But VB.NET 2005 already has an option to limit the application to one
        >instance...
        >>
        >
        >

        Comment

        • fniles

          #5
          Re: How can I check for thread existense ?

          And how can I use the semaphore type to detect multiple instances of the
          same thread ?

          "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
          news:u6FuuZO6HH A.2380@TK2MSFTN GP02.phx.gbl...
          Mutexes are normally used for detecting multiple instances of the same app
          >
          For threading you normally use the semaphore type
          >
          regards
          >
          michel
          >
          >
          "Spam Catcher" <spamhoneypot@r ogers.comschree f in bericht
          news:Xns99999EB 7D394Fusenethon eypotrogers@127 .0.0.1...
          >"fniles" <fniles@pfmail. comwrote in
          >news:#N7IxjN6H HA.600@TK2MSFTN GP05.phx.gbl:
          >>
          >>I am wondering if I can check the same thing for a thread.
          >>I have 1 thread that I do not want to create multiple instances of.
          >>Can I check whether the thread has exists before or not ?
          >>Thank you.
          >>
          >You would use a System Mutex to check if an instance is running. There
          >should be an example on google (used to be plenty, but I can't seem to
          >find
          >one!!!)
          >>
          >But VB.NET 2005 already has an option to limit the application to one
          >instance...
          >>
          >
          >

          Comment

          • Michel Posseth  [MCP]

            #6
            Re: How can I check for thread existense ?

            a thread can attempt to take ownership of the semaphore by calling the
            waitone method , if the current count is higher as zero ( count of the
            number of threads you allow ) , the semaphore returns inmediatly ,
            otherwise it will wait until anoher thread releases the semaphore , or until
            the optional timeout expires


            here is an MSDN example

            Limits the number of threads that can access a resource or pool of resources concurrently.


            P.s.

            remember to use a try finally block to ensure thet the semaphore is released
            even if the code throws an exception

            Like mutexes semaphores can have a name and be shared among processes when
            you try to create a sempahore that already exists the initial and
            maximumcounts are ignored


            HTH

            Michel



            "fniles" <fniles@pfmail. comschreef in bericht
            news:e3m28gZ6HH A.2208@TK2MSFTN GP06.phx.gbl...
            And how can I use the semaphore type to detect multiple instances of the
            same thread ?
            >
            "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
            news:u6FuuZO6HH A.2380@TK2MSFTN GP02.phx.gbl...
            >Mutexes are normally used for detecting multiple instances of the same
            >app
            >>
            >For threading you normally use the semaphore type
            >>
            >regards
            >>
            >michel
            >>
            >>
            >"Spam Catcher" <spamhoneypot@r ogers.comschree f in bericht
            >news:Xns99999E B7D394Fusenetho neypotrogers@12 7.0.0.1...
            >>"fniles" <fniles@pfmail. comwrote in
            >>news:#N7IxjN6 HHA.600@TK2MSFT NGP05.phx.gbl:
            >>>
            >>>I am wondering if I can check the same thing for a thread.
            >>>I have 1 thread that I do not want to create multiple instances of.
            >>>Can I check whether the thread has exists before or not ?
            >>>Thank you.
            >>>
            >>You would use a System Mutex to check if an instance is running. There
            >>should be an example on google (used to be plenty, but I can't seem to
            >>find
            >>one!!!)
            >>>
            >>But VB.NET 2005 already has an option to limit the application to one
            >>instance...
            >>>
            >>
            >>
            >
            >

            Comment

            • fniles

              #7
              Re: How can I check for thread existense ?

              Thank you.
              But, I have 2 different thread running on the application, and I would like
              to allow those 2 threads only. They both have a different name, say
              "ThreadA" and "ThreadB"
              I only want 1 instance of "ThreadA" and 1 instance of "ThreadB"
              Can I use semaphore to do that ?


              "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
              news:eVngoya6HH A.1208@TK2MSFTN GP05.phx.gbl...
              >a thread can attempt to take ownership of the semaphore by calling the
              >waitone method , if the current count is higher as zero ( count of the
              >number of threads you allow ) , the semaphore returns inmediatly ,
              >otherwise it will wait until anoher thread releases the semaphore , or
              >until the optional timeout expires
              >
              >
              here is an MSDN example
              >
              Limits the number of threads that can access a resource or pool of resources concurrently.

              >
              P.s.
              >
              remember to use a try finally block to ensure thet the semaphore is
              released even if the code throws an exception
              >
              Like mutexes semaphores can have a name and be shared among processes when
              you try to create a sempahore that already exists the initial and
              maximumcounts are ignored
              >
              >
              HTH
              >
              Michel
              >
              >
              >
              "fniles" <fniles@pfmail. comschreef in bericht
              news:e3m28gZ6HH A.2208@TK2MSFTN GP06.phx.gbl...
              >And how can I use the semaphore type to detect multiple instances of the
              >same thread ?
              >>
              >"Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
              >news:u6FuuZO6H HA.2380@TK2MSFT NGP02.phx.gbl.. .
              >>Mutexes are normally used for detecting multiple instances of the same
              >>app
              >>>
              >>For threading you normally use the semaphore type
              >>>
              >>regards
              >>>
              >>michel
              >>>
              >>>
              >>"Spam Catcher" <spamhoneypot@r ogers.comschree f in bericht
              >>news:Xns99999 EB7D394Fuseneth oneypotrogers@1 27.0.0.1...
              >>>"fniles" <fniles@pfmail. comwrote in
              >>>news:#N7IxjN 6HHA.600@TK2MSF TNGP05.phx.gbl:
              >>>>
              >>>>I am wondering if I can check the same thing for a thread.
              >>>>I have 1 thread that I do not want to create multiple instances of.
              >>>>Can I check whether the thread has exists before or not ?
              >>>>Thank you.
              >>>>
              >>>You would use a System Mutex to check if an instance is running. There
              >>>should be an example on google (used to be plenty, but I can't seem to
              >>>find
              >>>one!!!)
              >>>>
              >>>But VB.NET 2005 already has an option to limit the application to one
              >>>instance.. .
              >>>>
              >>>
              >>>
              >>
              >>
              >
              >

              Comment

              • Spam Catcher

                #8
                Re: How can I check for thread existense ?

                "fniles" <fniles@pfmail. comwrote in
                news:eqZHu1m6HH A.5136@TK2MSFTN GP02.phx.gbl:
                But, I have 2 different thread running on the application, and I would
                like to allow those 2 threads only. They both have a different name,
                say "ThreadA" and "ThreadB"
                I only want 1 instance of "ThreadA" and 1 instance of "ThreadB"
                Can you check the valud of the Thread Variable?

                If ThreadA is Nothing then
                'Do stuff here to initialize thread
                Else
                msgbox("Thread already created")
                End if

                Comment

                • fniles

                  #9
                  Re: How can I check for thread existense ?

                  Yes, I can do that. Thank you.
                  I was thinking to check for it inside the thread itself.
                  Can I do that ?

                  "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
                  news:Xns999B9E7 C9B548usenethon eypotrogers@127 .0.0.1...
                  "fniles" <fniles@pfmail. comwrote in
                  news:eqZHu1m6HH A.5136@TK2MSFTN GP02.phx.gbl:
                  >
                  >But, I have 2 different thread running on the application, and I would
                  >like to allow those 2 threads only. They both have a different name,
                  >say "ThreadA" and "ThreadB"
                  >I only want 1 instance of "ThreadA" and 1 instance of "ThreadB"
                  >
                  Can you check the valud of the Thread Variable?
                  >
                  If ThreadA is Nothing then
                  'Do stuff here to initialize thread
                  Else
                  msgbox("Thread already created")
                  End if

                  Comment

                  • Michel Posseth  [MCP]

                    #10
                    Re: How can I check for thread existense ?


                    Yes you can do that by using 2 semaphores


                    "fniles" <fniles@pfmail. comschreef in bericht
                    news:eqZHu1m6HH A.5136@TK2MSFTN GP02.phx.gbl...
                    Thank you.
                    But, I have 2 different thread running on the application, and I would
                    like to allow those 2 threads only. They both have a different name, say
                    "ThreadA" and "ThreadB"
                    I only want 1 instance of "ThreadA" and 1 instance of "ThreadB"
                    Can I use semaphore to do that ?
                    >
                    >
                    "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
                    news:eVngoya6HH A.1208@TK2MSFTN GP05.phx.gbl...
                    >>a thread can attempt to take ownership of the semaphore by calling the
                    >>waitone method , if the current count is higher as zero ( count of the
                    >>number of threads you allow ) , the semaphore returns inmediatly ,
                    >>otherwise it will wait until anoher thread releases the semaphore , or
                    >>until the optional timeout expires
                    >>
                    >>
                    >here is an MSDN example
                    >>
                    >http://msdn2.microsoft.com/en-us/lib...semaphore.aspx
                    >>
                    >P.s.
                    >>
                    >remember to use a try finally block to ensure thet the semaphore is
                    >released even if the code throws an exception
                    >>
                    >Like mutexes semaphores can have a name and be shared among processes
                    >when you try to create a sempahore that already exists the initial and
                    >maximumcount s are ignored
                    >>
                    >>
                    >HTH
                    >>
                    >Michel
                    >>
                    >>
                    >>
                    >"fniles" <fniles@pfmail. comschreef in bericht
                    >news:e3m28gZ6H HA.2208@TK2MSFT NGP06.phx.gbl.. .
                    >>And how can I use the semaphore type to detect multiple instances of the
                    >>same thread ?
                    >>>
                    >>"Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
                    >>news:u6FuuZO6 HHA.2380@TK2MSF TNGP02.phx.gbl. ..
                    >>>Mutexes are normally used for detecting multiple instances of the same
                    >>>app
                    >>>>
                    >>>For threading you normally use the semaphore type
                    >>>>
                    >>>regards
                    >>>>
                    >>>michel
                    >>>>
                    >>>>
                    >>>"Spam Catcher" <spamhoneypot@r ogers.comschree f in bericht
                    >>>news:Xns9999 9EB7D394Fusenet honeypotrogers@ 127.0.0.1...
                    >>>>"fniles" <fniles@pfmail. comwrote in
                    >>>>news:#N7Ixj N6HHA.600@TK2MS FTNGP05.phx.gbl :
                    >>>>>
                    >>>>>I am wondering if I can check the same thing for a thread.
                    >>>>>I have 1 thread that I do not want to create multiple instances of.
                    >>>>>Can I check whether the thread has exists before or not ?
                    >>>>>Thank you.
                    >>>>>
                    >>>>You would use a System Mutex to check if an instance is running. There
                    >>>>should be an example on google (used to be plenty, but I can't seem to
                    >>>>find
                    >>>>one!!!)
                    >>>>>
                    >>>>But VB.NET 2005 already has an option to limit the application to one
                    >>>>instance. ..
                    >>>>>
                    >>>>
                    >>>>
                    >>>
                    >>>
                    >>
                    >>
                    >
                    >

                    Comment

                    Working...