tags:

views:

275

answers:

1

I have gone through the code of mutex, but it is giving me errors like:

public override void dispose(bool disposing); no suitable method found to dispose

This is occurring on initialize component, so I have commented that portion, but the major error I'm facing is on the design view part:

The designer cannot process the code at line 26: throw new NotImplementedException(); The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again.

I cannot view form.cs[design]. How can I fix this?

Program.cs:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PU;

namespace WindowsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            // If this program is already running, set focus
            // to that instance and quit.
            if (ProcessUtils.ThisProcessIsAlreadyRunning())
            {
                // "Form1" is the caption (Text property) of the main form.
                ProcessUtils.SetFocusToPreviousInstance("Form1");
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
}

ProcessUtils.cs:

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PU
{
    /// Summary description for ProcessUtils.
    public static class ProcessUtils
    {
        private static Mutex mutex = null;

        /// Determine if the current process is already running
        public static bool ThisProcessIsAlreadyRunning()
        {
            // Only want to call this method once, at startup.
            Debug.Assert(mutex == null);

            // createdNew needs to be false in .Net 2.0, otherwise, if another instance of
            // this program is running, the Mutex constructor will block, and then throw 
            // an exception if the other instance is shut down.
            bool createdNew = false;

            mutex = new Mutex(false, Application.ProductName, out createdNew);

            Debug.Assert(mutex != null);

            return !createdNew;
        }

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern bool IsIconic(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        const int SW_RESTORE = 9;

        [DllImport("user32.dll")]
        static extern IntPtr GetLastActivePopup(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern bool IsWindowEnabled(IntPtr hWnd);

        /// Set focus to the previous instance of the specified program.
        public static void SetFocusToPreviousInstance(string windowCaption)
        {
            // Look for previous instance of this program.
            IntPtr hWnd = FindWindow(null, windowCaption);

            // If a previous instance of this program was found...
            if (hWnd != null)
            {
                // Is it displaying a popup window?
                IntPtr hPopupWnd = GetLastActivePopup(hWnd);

                // If so, set focus to the popup window. Otherwise set focus
                // to the program's main window.
                if (hPopupWnd != null && IsWindowEnabled(hPopupWnd))
                {
                    hWnd = hPopupWnd;
                }

                SetForegroundWindow(hWnd);

                // If program is minimized, restore it.
                if (IsIconic(hWnd))
                {
                    ShowWindow(hWnd, SW_RESTORE);
                }
            }
        }
    }
}
+1  A: 

If you're really trying to override the "dispose" method then it's a matter of casing - it's Dispose, not dispose. C# is case-sensitive.

As for the rest of the problems, it's hard to say as you really haven't given enough information as to what you're doing. It would help if you'd start by telling us more about your situation. What have you done, exactly? Where does the mutex come into this?

Jon Skeet
yes i know c# is case sensitive i have written Dispose insead of dispose but still not got the solutionim giving u the whole code i have tried out just go through it and then look forward to the errors im facing in it
zoya
Program.cs:using System;using System.Collections.Generic;using System.Windows.Forms;using PU;namespace WindowsApplication1{static class Program{[STAThread]static void Main(){// If this program is already running, set focus// to that instance and quit.if (ProcessUtils.ThisProcessIsAlreadyRunning()){// "Form1" is the caption (Text property) of the main form.ProcessUtils.SetFocusToPreviousInstance("Form1");}else{Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());}}}
zoya
i don't know how to put the entire code on stackover plz tell me the method so that i can put the detail code on it....im new to stackover
zoya
You edit the *question*, rather than adding it to comments.
Jon Skeet
ok but i have posted a new question in that i have given complete code that i have executed the question is:-how to run the mutex code in c#.net on button click event?
zoya
i need to execute the third party exe on the button click and on that i need to apply this mutex code which will provide my exe to run only once, that is i need to to run a single instance of my program, and if my program is already running then it should focus and reopen that again on the button click rather than making multiple entries.
zoya
And which part is not working ? Detecting the 2nd instance or bringing it to the foreground ? The last part is always difficult: Windows is quite reluctant on that issue, as some user testing showed that people don't like to have an app pop up while they are doing something else.
Timores