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

如何在c#.net中的按钮单击事件中运行互斥代码?,c#,.net,mutex,C#,.net,Mutex,这是我的代码,我曾试图在我的c#.net 3.5程序中运行,但出现了错误。我做错了什么 错误CS0115:“Form1.Dispose(bool)”:未找到可重写的合适方法 这是我得到错误的代码: protected override void Dispose(bool disposing) { if (disposing) { this._userConnectionOption = null; this._poolGroup = null;

这是我的代码,我曾试图在我的c#.net 3.5程序中运行,但出现了错误。我做错了什么

错误CS0115:“Form1.Dispose(bool)”:未找到可重写的合适方法

这是我得到错误的代码:

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOption = null;
        this._poolGroup = null;
        this.close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}
实际编码从这里开始:

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);
                }
            }
        }
    }

使用
true
作为第一个参数创建互斥锁

并插入

if(!createdNew)
    mutex.Close();
以前

return !createdNew;

默认情况下,windows“窗体”(在命名空间System.windows.Forms中)实现IDisposable(在命名空间系统中)

但是从错误消息来看,您的“Form1”似乎没有扩展“Form”类。因此,在您的代码中,编译器正在抱怨缺少“overridable”方法


请同时发布您的“Form1”代码好吗?

“我有错误”不准确。请准确地说出您遇到的错误。代码太多,请发布产生错误的代码段和实际错误。我已经阅读了互斥体的代码。。但它给了我一些错误,比如:-公共覆盖无效处置(bool disposing);未找到合适的处理方法。。这是在初始化组件。。因此,我对这一部分进行了评论。。但我面临的主要错误是在设计视图部分:-设计器无法处理第26行的代码:抛出新的NotImplementedException();“InitializeComponent”方法中的代码由设计器生成,不应手动修改。请删除所有更改,然后再次尝试打开设计器。我无法查看form.cs[design]plz help me out您还没有发布Dispose方法(同样,案例在C#中很重要-所以请准确发布您键入的内容,而不是忽略案例)。当错误发生在您没有显示的某些代码中时,这是一个非常强烈的建议,我们将无法提供太多帮助。错误CS0115:'Form1.Dispose(bool)':找不到合适的方法来重写这是我得到错误保护的重写void Dispose(bool disposing){if(disposing)的代码{this.\u userConnectionOption=null;this.\u poolGroup=null;this.close();}this.DisposeMe(disposing);base.Dispose(disposing);}谢谢,先生,但是我的错误仍然没有解决,我需要知道我在以下错误中遇到的错误:-error CS0115:'Form1.Dispose(bool)“:找不到合适的方法来重写我猜您是从IDisposable派生表单的,并且没有实现Dispose()。如何实现Dispose plz?”?