相关疑难解决方法(0)

C#SetForegroundWindow不工作

我已经限制我的C#windows应用程序只允许一次运行一个实例,使用这个问题如何强制C#.net应用程序在Windows中只运行一个实例?

它运行良好,并且不允许同时运行多个应用程序实例.

问题是如果用户试图打开应用程序的第二个实例,我希望当前活动的实例能够出现在前面.

我工作的问题似乎解决了这个问题,但它对我不起作用.

我认为这是因为我的应用程序不符合允许该方法的标准:SetForegroundWindow 工作.

我的问题是,我怎样才能做到这一点.我的代码如下:

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

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

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool createdNew = true;

            using (Mutex mutex = new Mutex(true, "RTRFIDListener_Client", out createdNew))
            {
                if (createdNew)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new frm_Main());
                }
                else
                {
                    Process current …
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
1
解决办法
1967
查看次数

标签 统计

c# ×1