Results 1 to 13 of 13

Thread: [RESOLVED] prevent instance of application

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Resolved [RESOLVED] prevent instance of application

    hi guys!! is there anyway for me to prevent any other intance of my windows application to run? cause i can run 2 or more of my windows application which i dont want to happen. thans in advance guys!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: prevent instance of application

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: prevent instance of application

    I come up the code below but still I can run another instance of my apps. btw, I call that method under Form_load. can you help me to figure out whats wrong...

    Code:
            public void chk_instance()
            {
                string mutexName = Assembly.GetEntryAssembly().FullName.ToString();
                bool grantedOwnership;
                Mutex SingleInstanceMutex = new Mutex(true, mutexName,out grantedOwnership);
                if (! grantedOwnership)
                {
                    MessageBox.Show("Another instance of this application is open", "BCMD", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                }
            }
    Last edited by daimous; Oct 25th, 2006 at 06:25 PM.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: prevent instance of application

    Try this
    Code:
            static bool running = false;
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                if (!running)
                {
                    running = true;
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
            }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: prevent instance of application

    I was under the impression that you could set the "single instance" switch in the project properties window.

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: prevent instance of application

    Quote Originally Posted by Pasvorto
    I was under the impression that you could set the "single instance" switch in the project properties window.
    That option exists only in VB2005
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: prevent instance of application

    Oh, my bad.

  8. #8

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: prevent instance of application

    does VC#2005 has that option? i've tried to look at it but i haven't seen that option.
    Last edited by daimous; Oct 30th, 2006 at 07:01 PM.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: prevent instance of application

    Quote Originally Posted by daimous
    I come up the code below but still I can run another instance of my apps. btw, I call that method under Form_load. can you help me to figure out whats wrong...

    Code:
            public void chk_instance()
            {
                string mutexName = Assembly.GetEntryAssembly().FullName.ToString();
                bool grantedOwnership;
                Mutex SingleInstanceMutex = new Mutex(true, mutexName,out grantedOwnership);
                if (! grantedOwnership)
                {
                    MessageBox.Show("Another instance of this application is open", "BCMD", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                }
            }
    The link I posted specifically places the code to prevent multiple instances in the Main method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: prevent instance of application

    I created a new project, edited the Main method to the following and voila.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace SingleInstanceTest
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                string mutexName = "3af65962-66bf-4135-bc5a-b0f986d8e995";
                bool grantedOwnership;
                System.Threading.Mutex singleInstanceMutex = new System.Threading.Mutex(true, mutexName, out grantedOwnership);
    
                try
                {
                    if (grantedOwnership)
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new Form1());
                    }
                }
                finally
                {
                    singleInstanceMutex.Close();
                }
            }
        }
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: prevent instance of application

    Sorry to ask JM but i dont have an idea where to place your code. Is
    Code:
    static void Main()
    is automatically generated when i create a project or i need to hard-code it manually?

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: prevent instance of application

    Yes, the Main method is automatically generated. As you can see from the code it is a member of the Program class. The Program class is declared in the Program.cs code file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: prevent instance of application

    Thanks! it is working now.

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