简体   繁体   English

如何在C#程序中使用互斥代码?

[英]How to use the mutex code in a C# program?

I have gone through the code of mutex, but it is giving me errors like: 我已经完成了互斥锁的代码,但它给了我错误,如:

public override void dispose(bool disposing); 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(); 设计者无法在第26行处理代码:throw new NotImplementedException(); The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. 方法'InitializeComponent'中的代码由设计者生成,不应手动修改。 Please remove any changes and try opening the designer again. 请删除所有更改,然后再次尝试打开设计器。

I cannot view form.cs[design]. 我无法查看form.cs [design]。 How can I fix this? 我怎样才能解决这个问题?

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

If you're really trying to override the "dispose" method then it's a matter of casing - it's Dispose , not dispose . 如果你真的试图覆盖“处置”方法那么它就是套管问题 - 它是Dispose ,而不是dispose C# is case-sensitive. C#区分大小写。

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? 互斥体在哪里进入?

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

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