What is wrong with this mutex code

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

    What is wrong with this mutex code

    I build the solution.

    In the bin folder I run the .exe

    Twice

    It runs each time.

    Why doesn't the below code abort the second run?



    Thanks

    Shared Function Main(ByVal cmdArgs() As String) As Integer

    Dim mutexWasCreated As Boolean

    Using mutx As New Mutex(True, "TestJunk", mutexWasCreated )



    If Not mutexWasCreated Then Return 1

    End Using

    Try

    Application.Ena bleVisualStyles ()

    Application.Run (New FormStudioMdi)

    Catch ex As Exception


  • Stephany Young

    #2
    Re: What is wrong with this mutex code

    Becaue you are releasing of the mutex way too early.

    If you're going to have your Application.Run (...) in a Try/Catch/End Try
    block then extend it to include a Finally section and release it there.


    "Academia" <academiaNOSPAM @a-znet.comwrote in message
    news:uGrpz3OTIH A.5360@TK2MSFTN GP03.phx.gbl...
    >I build the solution.
    >
    In the bin folder I run the .exe
    >
    Twice
    >
    It runs each time.
    >
    Why doesn't the below code abort the second run?
    >
    >
    >
    Thanks
    >
    Shared Function Main(ByVal cmdArgs() As String) As Integer
    >
    Dim mutexWasCreated As Boolean
    >
    Using mutx As New Mutex(True, "TestJunk", mutexWasCreated )
    >
    >
    >
    If Not mutexWasCreated Then Return 1
    >
    End Using
    >
    Try
    >
    Application.Ena bleVisualStyles ()
    >
    Application.Run (New FormStudioMdi)
    >
    Catch ex As Exception
    >
    >

    Comment

    • kimiraikkonen

      #3
      Re: What is wrong with this mutex code

      On Jan 2, 5:17 am, "Academia" <academiaNOS... @a-znet.comwrote:
      I build the solution.
      >
      In the bin folder I run the .exe
      >
      Twice
      >
      It runs each time.
      >
      Why doesn't the below code abort the second run?
      >
      Thanks
      >
      Shared Function Main(ByVal cmdArgs() As String) As Integer
      >
      Dim mutexWasCreated As Boolean
      >
      Using mutx As New Mutex(True, "TestJunk", mutexWasCreated )
      >
      If Not mutexWasCreated Then Return 1
      >
      End Using
      >
      Try
      >
      Application.Ena bleVisualStyles ()
      >
      Application.Run (New FormStudioMdi)
      >
      Catch ex As Exception
      "Finally" (optional) and "end try" (required) parts are missing. Also
      you've defined "Catch" with no exception display method, you may
      throw the exception.

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: What is wrong with this mutex code

        "Academia" <academiaNOSPAM @a-znet.comschrieb :
        In the bin folder I run the .exe
        >
        Twice
        >
        It runs each time.
        >
        Why doesn't the below code abort the second run?
        <URL:http://www.yoda.arachs ys.com/csharp/faq/#one.applicatio n.instance>

        In VB 2005 and VB 2008 you can make your application single-instance by
        enabling the application framework and checking a checkbox in the
        application framework properties (available in "My Project").

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • Academia

          #5
          Re: What is wrong with this mutex code

          I've never used a mutex so I'm going to finish the way I started and then
          look at your suggestion.

          The site you referenced says I need not release the mutex and also suggest
          using a static variable.

          Is that what you would do?


          Thanks


          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
          news:uGSzwbTTIH A.3516@TK2MSFTN GP02.phx.gbl...
          "Academia" <academiaNOSPAM @a-znet.comschrieb :
          >In the bin folder I run the .exe
          >>
          >Twice
          >>
          >It runs each time.
          >>
          >Why doesn't the below code abort the second run?
          >
          <URL:http://www.yoda.arachs ys.com/csharp/faq/#one.applicatio n.instance>
          >
          In VB 2005 and VB 2008 you can make your application single-instance by
          enabling the application framework and checking a checkbox in the
          application framework properties (available in "My Project").
          >
          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          • Academia

            #6
            Re: What is wrong with this mutex code


            "Stephany Young" <noone@localhos twrote in message
            news:e4wpWQPTIH A.6060@TK2MSFTN GP05.phx.gbl...
            Becaue you are releasing of the mutex way too early.
            >
            If you're going to have your Application.Run (...) in a Try/Catch/End Try
            block then extend it to include a Finally section and release it there.
            >
            I moved the End Using. Is that a good way?

            Also, does it make sense to have the two message boxes.

            I don't know a lot about the difference between two exceptions.

            The revised code is below.



            Thanks for the help

            Shared Function Main(ByVal cmdArgs() As String) As Integer

            Dim mutexWasCreated As Boolean

            Using mutx As New Mutex(True, "TestJunk", mutexWasCreated )



            If Not mutexWasCreated Then Return 1

            Try

            Application.Ena bleVisualStyles ()

            Application.Run (New FormStudioMdi)

            Catch ex As Exception

            Utility.WriteSt ackTrace(ex)

            If ex.InnerExcepti on IsNot Nothing Then
            MessageBox.Show (ex.InnerExcept ion.Message, " Experienced An Unhandled Inner
            Error And Must Exit", MessageBoxButto ns.OK)

            If ex.GetBaseExcep tion IsNot Nothing Then
            MessageBox.Show (ex.GetBaseExce ption.Message, " Experienced A BaseException
            And Must Exit", MessageBoxButto ns.OK)

            Return 1

            End Try

            Return 0

            End Using

            End Function


            Comment

            • Academia

              #7
              Re: What is wrong with this mutex code


              I should have sent
              Catch ex As Exception ..snip...
              There is code I didn't show.
              Maybe you could comment on my reply to Stephany Young which shows all the
              code.

              Thanks
              >
              "Finally" (optional) and "end try" (required) parts are missing. Also
              you've defined "Catch" with no exception display method, you may
              throw the exception.

              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: What is wrong with this mutex code

                "Academia" <academiaNOSPAM @a-znet.comschrieb :
                The site you referenced says I need not release the mutex and also suggest
                using a static variable.
                >
                Is that what you would do?
                Yes.

                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                Comment

                • Academia

                  #9
                  Re: What is wrong with this mutex code

                  thanks


                  "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
                  news:uwLLqwUTIH A.5516@TK2MSFTN GP02.phx.gbl...
                  "Academia" <academiaNOSPAM @a-znet.comschrieb :
                  >The site you referenced says I need not release the mutex and also
                  >suggest using a static variable.
                  >>
                  >Is that what you would do?
                  >
                  Yes.
                  >
                  --
                  M S Herfried K. Wagner
                  M V P <URL:http://dotnet.mvps.org/>
                  V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                  Comment

                  Working...