简体   繁体   English

如何在按钮单击事件中在c#.net中运行互斥代码?

[英]How to run the mutex code in c#.net on button click event?

This is my code which I had tried to run in my c# .net 3.5 program but I'm getting errors. 这是我尝试在c#.net 3.5程序中运行的代码,但出现错误。 What am I doing wrong? 我究竟做错了什么?

error CS0115: 'Form1.Dispose(bool)': no suitable method found to override 错误CS0115:“ Form1.Dispose(bool)”:找不到合适的方法来覆盖

This is the code where i got the error: 这是我得到错误的代码:

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOption = null;
        this._poolGroup = null;
        this.close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}

The actual coding starts from here: 实际的编码从这里开始:

Program.cs: 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: 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);
                }
            }
        }
    }

Create your mutex with true as the first param. true作为第一个参数创建互斥量。

And insert 并插入

if(!createdNew)
    mutex.Close();

before 之前

return !createdNew;

By default, a windows 'Form' (in namespace System.Windows.Forms) implements IDisposable (in namespace System) 默认情况下,Windows“窗体”(在名称空间System.Windows.Forms中)实现IDisposable(在名称空间System中)

But from the error message it looks like your "Form1" does not extend the 'Form' class. 但是从错误消息看来,您的“ Form1”没有扩展“ Form”类。 Hence in your code the compiler is complaining about the absence of 'over-riddable' method. 因此,在您的代码中,编译器抱怨缺少“ over-riddable”方法。

Could you please also post your 'Form1' code ? 您还可以发布您的“ Form1”代码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM