Many instances of the same program

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

    #1

    Many instances of the same program

    I need to run more then 1 instance of the same program but also I need to
    know which instance is running.
    In C++ code was:
    int nInstance = 0;
    char szAppName[32];


    m_dwMutexReturn = ERROR_ALREADY_E XISTS;
    while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
    MAXINSTANCES ))
    {
    nInstance++;
    itoa( nInstance, szTemp, 10 );
    strcpy( szAppName, "ProductEng ine" );
    strcat( szAppName, szTemp );
    m_hMutex = CreateMutex( NULL, TRUE, szAppName );
    m_dwMutexReturn = GetLastError();
    }

    if ( nInstance == MAXINSTANCES )
    m_sState->nInstance = 1;
    else
    m_sState->nInstance = nInstance;
    but I can't port this code to C#.
    Somebody knows how to do it?

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Many instances of the same program

    Bronislav,

    There are a number of examples of how to do this on the web. Have you
    taken a look at the Mutex class? It will give you everything you need to do
    to replicate this behavior.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
    news:2D3B8599-9DA9-4D0C-A526-2FAE00EBB7A2@mi crosoft.com...
    >I need to run more then 1 instance of the same program but also I need to
    know which instance is running.
    In C++ code was:
    int nInstance = 0;
    char szAppName[32];
    >
    >
    m_dwMutexReturn = ERROR_ALREADY_E XISTS;
    while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
    MAXINSTANCES ))
    {
    nInstance++;
    itoa( nInstance, szTemp, 10 );
    strcpy( szAppName, "ProductEng ine" );
    strcat( szAppName, szTemp );
    m_hMutex = CreateMutex( NULL, TRUE, szAppName );
    m_dwMutexReturn = GetLastError();
    }
    >
    if ( nInstance == MAXINSTANCES )
    m_sState->nInstance = 1;
    else
    m_sState->nInstance = nInstance;
    but I can't port this code to C#.
    Somebody knows how to do it?
    >

    Comment

    • =?Utf-8?B?QnJvbmlzbGF2?=

      #3
      Re: Many instances of the same program

      The Mutex class in my understanding is not complete analog of the CreateMutex
      API
      because it not allowed to create difference instances of the programm with
      the different name and I need exactly this.

      "Nicholas Paldino [.NET/C# MVP]" wrote:
      Bronislav,
      >
      There are a number of examples of how to do this on the web. Have you
      taken a look at the Mutex class? It will give you everything you need to do
      to replicate this behavior.
      >
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m
      >
      "Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
      news:2D3B8599-9DA9-4D0C-A526-2FAE00EBB7A2@mi crosoft.com...
      I need to run more then 1 instance of the same program but also I need to
      know which instance is running.
      In C++ code was:
      int nInstance = 0;
      char szAppName[32];


      m_dwMutexReturn = ERROR_ALREADY_E XISTS;
      while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
      MAXINSTANCES ))
      {
      nInstance++;
      itoa( nInstance, szTemp, 10 );
      strcpy( szAppName, "ProductEng ine" );
      strcat( szAppName, szTemp );
      m_hMutex = CreateMutex( NULL, TRUE, szAppName );
      m_dwMutexReturn = GetLastError();
      }

      if ( nInstance == MAXINSTANCES )
      m_sState->nInstance = 1;
      else
      m_sState->nInstance = nInstance;
      but I can't port this code to C#.
      Somebody knows how to do it?
      >
      >
      >

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Many instances of the same program

        Bronislav,

        You can call the static OpenExisting method on the Mutex class to get a
        named mutex instance. If it doesn't exist, then you can catch the
        WaitHandleCanno tBeOpenedExcept ion instance that is thrown and then create a
        new one.

        Now, you do run the risk of running into a race condition here, as two
        instances of the app can try to open a mutex that doesn't exist, which is
        different from the call to CreateMutex. If you want to avoid this race
        condition, declare the CreateMutex API function and call it through the
        P/Invoke layer, getting the handle. Then, create a new Mutex instance, and
        assign the handle to that if you want to use it.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
        news:7FF9826E-4D80-40B6-8B9F-8E5164F9706E@mi crosoft.com...
        The Mutex class in my understanding is not complete analog of the
        CreateMutex
        API
        because it not allowed to create difference instances of the programm with
        the different name and I need exactly this.
        >
        "Nicholas Paldino [.NET/C# MVP]" wrote:
        >
        >Bronislav,
        >>
        > There are a number of examples of how to do this on the web. Have
        >you
        >taken a look at the Mutex class? It will give you everything you need to
        >do
        >to replicate this behavior.
        >>
        >>
        >--
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >>
        >"Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
        >news:2D3B859 9-9DA9-4D0C-A526-2FAE00EBB7A2@mi crosoft.com...
        >I need to run more then 1 instance of the same program but also I need
        >to
        know which instance is running.
        In C++ code was:
        int nInstance = 0;
        char szAppName[32];
        >
        >
        m_dwMutexReturn = ERROR_ALREADY_E XISTS;
        while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
        MAXINSTANCES ))
        {
        nInstance++;
        itoa( nInstance, szTemp, 10 );
        strcpy( szAppName, "ProductEng ine" );
        strcat( szAppName, szTemp );
        m_hMutex = CreateMutex( NULL, TRUE, szAppName );
        m_dwMutexReturn = GetLastError();
        }
        >
        if ( nInstance == MAXINSTANCES )
        m_sState->nInstance = 1;
        else
        m_sState->nInstance = nInstance;
        but I can't port this code to C#.
        Somebody knows how to do it?
        >
        >>
        >>
        >>

        Comment

        • =?Utf-8?B?QnJvbmlzbGF2?=

          #5
          Re: Many instances of the same program

          I tried to do exactly that but I cannot find how to call CreateMutex through
          p-invoke
          I tried both declaretions

          /*
          [DllImport("kern el32.dll", SetLastError=tr ue)]
          private static extern int CreateMutexA(
          ref SECURITY_ATTRIB UTES lpMutexAttribut es,
          int bInitialOwner,
          string lpName);
          */
          [DllImport("kern el32.dll", SetLastError=tr ue)]
          private static extern System.IntPtr CreateMutexA(
          out int lpMutexAttribut es,
          bool bInitialOwner,
          string lpName);
          but when I call CreateMutex
          lhndlRetHandle = CreateMutexA(nu ll, true, lstrAppName);
          It give me error that first parameter cannot be NULL
          Thanks.


          "Nicholas Paldino [.NET/C# MVP]" wrote:
          Bronislav,
          >
          You can call the static OpenExisting method on the Mutex class to get a
          named mutex instance. If it doesn't exist, then you can catch the
          WaitHandleCanno tBeOpenedExcept ion instance that is thrown and then create a
          new one.
          >
          Now, you do run the risk of running into a race condition here, as two
          instances of the app can try to open a mutex that doesn't exist, which is
          different from the call to CreateMutex. If you want to avoid this race
          condition, declare the CreateMutex API function and call it through the
          P/Invoke layer, getting the handle. Then, create a new Mutex instance, and
          assign the handle to that if you want to use it.
          >
          >
          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m
          >
          "Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
          news:7FF9826E-4D80-40B6-8B9F-8E5164F9706E@mi crosoft.com...
          The Mutex class in my understanding is not complete analog of the
          CreateMutex
          API
          because it not allowed to create difference instances of the programm with
          the different name and I need exactly this.

          "Nicholas Paldino [.NET/C# MVP]" wrote:
          Bronislav,
          >
          There are a number of examples of how to do this on the web. Have
          you
          taken a look at the Mutex class? It will give you everything you need to
          do
          to replicate this behavior.
          >
          >
          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m
          >
          "Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
          news:2D3B8599-9DA9-4D0C-A526-2FAE00EBB7A2@mi crosoft.com...
          I need to run more then 1 instance of the same program but also I need
          to
          know which instance is running.
          In C++ code was:
          int nInstance = 0;
          char szAppName[32];


          m_dwMutexReturn = ERROR_ALREADY_E XISTS;
          while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
          MAXINSTANCES ))
          {
          nInstance++;
          itoa( nInstance, szTemp, 10 );
          strcpy( szAppName, "ProductEng ine" );
          strcat( szAppName, szTemp );
          m_hMutex = CreateMutex( NULL, TRUE, szAppName );
          m_dwMutexReturn = GetLastError();
          }

          if ( nInstance == MAXINSTANCES )
          m_sState->nInstance = 1;
          else
          m_sState->nInstance = nInstance;
          but I can't port this code to C#.
          Somebody knows how to do it?

          >
          >
          >
          >
          >
          >

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Many instances of the same program

            Well, you can declare lpMutexAttribut es as IntPtr if you are going to
            pass null. Also, you should declare the bInitialOwner parameter as bool,
            and you should indicate the version of the function you want to use in the
            DllImport attribute and you should set the return type to IntPtr (since it
            is a handle). Putting it all together, you should have:

            [DllImport("kern el32.dll", SetLastError=Tr ue, CharSet=CharSet .Auto)]
            private static extern IntPtr CreateMutex(Int Ptr lpMutexAttribut es, bool
            bInitialOwner, string lpName);

            Then, you can pass IntPtr.Zero to the first parameter.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
            news:0CD9F855-C2F6-4D24-9208-917BF33DEEC4@mi crosoft.com...
            >I tried to do exactly that but I cannot find how to call CreateMutex
            >through
            p-invoke
            I tried both declaretions
            >
            /*
            [DllImport("kern el32.dll", SetLastError=tr ue)]
            private static extern int CreateMutexA(
            ref SECURITY_ATTRIB UTES lpMutexAttribut es,
            int bInitialOwner,
            string lpName);
            */
            [DllImport("kern el32.dll", SetLastError=tr ue)]
            private static extern System.IntPtr CreateMutexA(
            out int lpMutexAttribut es,
            bool bInitialOwner,
            string lpName);
            but when I call CreateMutex
            lhndlRetHandle = CreateMutexA(nu ll, true, lstrAppName);
            It give me error that first parameter cannot be NULL
            Thanks.
            >
            >
            "Nicholas Paldino [.NET/C# MVP]" wrote:
            >
            >Bronislav,
            >>
            > You can call the static OpenExisting method on the Mutex class to get
            >a
            >named mutex instance. If it doesn't exist, then you can catch the
            >WaitHandleCann otBeOpenedExcep tion instance that is thrown and then create
            >a
            >new one.
            >>
            > Now, you do run the risk of running into a race condition here, as
            >two
            >instances of the app can try to open a mutex that doesn't exist, which is
            >different from the call to CreateMutex. If you want to avoid this race
            >condition, declare the CreateMutex API function and call it through the
            >P/Invoke layer, getting the handle. Then, create a new Mutex instance,
            >and
            >assign the handle to that if you want to use it.
            >>
            >>
            >--
            > - Nicholas Paldino [.NET/C# MVP]
            > - mvp@spam.guard. caspershouse.co m
            >>
            >"Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
            >news:7FF9826 E-4D80-40B6-8B9F-8E5164F9706E@mi crosoft.com...
            The Mutex class in my understanding is not complete analog of the
            CreateMutex
            API
            because it not allowed to create difference instances of the programm
            with
            the different name and I need exactly this.
            >
            "Nicholas Paldino [.NET/C# MVP]" wrote:
            >
            >Bronislav,
            >>
            > There are a number of examples of how to do this on the web. Have
            >you
            >taken a look at the Mutex class? It will give you everything you need
            >to
            >do
            >to replicate this behavior.
            >>
            >>
            >--
            > - Nicholas Paldino [.NET/C# MVP]
            > - mvp@spam.guard. caspershouse.co m
            >>
            >"Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
            >news:2D3B859 9-9DA9-4D0C-A526-2FAE00EBB7A2@mi crosoft.com...
            >I need to run more then 1 instance of the same program but also I
            >need
            >to
            know which instance is running.
            In C++ code was:
            int nInstance = 0;
            char szAppName[32];
            >
            >
            m_dwMutexReturn = ERROR_ALREADY_E XISTS;
            while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
            MAXINSTANCES ))
            {
            nInstance++;
            itoa( nInstance, szTemp, 10 );
            strcpy( szAppName, "ProductEng ine" );
            strcat( szAppName, szTemp );
            m_hMutex = CreateMutex( NULL, TRUE, szAppName );
            m_dwMutexReturn = GetLastError();
            }
            >
            if ( nInstance == MAXINSTANCES )
            m_sState->nInstance = 1;
            else
            m_sState->nInstance = nInstance;
            but I can't port this code to C#.
            Somebody knows how to do it?
            >
            >>
            >>
            >>
            >>
            >>
            >>

            Comment

            • =?Utf-8?B?QnJvbmlzbGF2?=

              #7
              Re: Many instances of the same program

              Thanks

              "Nicholas Paldino [.NET/C# MVP]" wrote:
              Well, you can declare lpMutexAttribut es as IntPtr if you are going to
              pass null. Also, you should declare the bInitialOwner parameter as bool,
              and you should indicate the version of the function you want to use in the
              DllImport attribute and you should set the return type to IntPtr (since it
              is a handle). Putting it all together, you should have:
              >
              [DllImport("kern el32.dll", SetLastError=Tr ue, CharSet=CharSet .Auto)]
              private static extern IntPtr CreateMutex(Int Ptr lpMutexAttribut es, bool
              bInitialOwner, string lpName);
              >
              Then, you can pass IntPtr.Zero to the first parameter.
              >
              >
              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m
              >
              "Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
              news:0CD9F855-C2F6-4D24-9208-917BF33DEEC4@mi crosoft.com...
              I tried to do exactly that but I cannot find how to call CreateMutex
              through
              p-invoke
              I tried both declaretions

              /*
              [DllImport("kern el32.dll", SetLastError=tr ue)]
              private static extern int CreateMutexA(
              ref SECURITY_ATTRIB UTES lpMutexAttribut es,
              int bInitialOwner,
              string lpName);
              */
              [DllImport("kern el32.dll", SetLastError=tr ue)]
              private static extern System.IntPtr CreateMutexA(
              out int lpMutexAttribut es,
              bool bInitialOwner,
              string lpName);
              but when I call CreateMutex
              lhndlRetHandle = CreateMutexA(nu ll, true, lstrAppName);
              It give me error that first parameter cannot be NULL
              Thanks.


              "Nicholas Paldino [.NET/C# MVP]" wrote:
              Bronislav,
              >
              You can call the static OpenExisting method on the Mutex class to get
              a
              named mutex instance. If it doesn't exist, then you can catch the
              WaitHandleCanno tBeOpenedExcept ion instance that is thrown and then create
              a
              new one.
              >
              Now, you do run the risk of running into a race condition here, as
              two
              instances of the app can try to open a mutex that doesn't exist, which is
              different from the call to CreateMutex. If you want to avoid this race
              condition, declare the CreateMutex API function and call it through the
              P/Invoke layer, getting the handle. Then, create a new Mutex instance,
              and
              assign the handle to that if you want to use it.
              >
              >
              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m
              >
              "Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
              news:7FF9826E-4D80-40B6-8B9F-8E5164F9706E@mi crosoft.com...
              The Mutex class in my understanding is not complete analog of the
              CreateMutex
              API
              because it not allowed to create difference instances of the programm
              with
              the different name and I need exactly this.

              "Nicholas Paldino [.NET/C# MVP]" wrote:

              Bronislav,
              >
              There are a number of examples of how to do this on the web. Have
              you
              taken a look at the Mutex class? It will give you everything you need
              to
              do
              to replicate this behavior.
              >
              >
              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m
              >
              "Bronislav" <Bronislav@disc ussions.microso ft.comwrote in message
              news:2D3B8599-9DA9-4D0C-A526-2FAE00EBB7A2@mi crosoft.com...
              I need to run more then 1 instance of the same program but also I
              need
              to
              know which instance is running.
              In C++ code was:
              int nInstance = 0;
              char szAppName[32];


              m_dwMutexReturn = ERROR_ALREADY_E XISTS;
              while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
              MAXINSTANCES ))
              {
              nInstance++;
              itoa( nInstance, szTemp, 10 );
              strcpy( szAppName, "ProductEng ine" );
              strcat( szAppName, szTemp );
              m_hMutex = CreateMutex( NULL, TRUE, szAppName );
              m_dwMutexReturn = GetLastError();
              }

              if ( nInstance == MAXINSTANCES )
              m_sState->nInstance = 1;
              else
              m_sState->nInstance = nInstance;
              but I can't port this code to C#.
              Somebody knows how to do it?

              >
              >
              >
              >
              >
              >
              >
              >
              >

              Comment

              Working...