首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# SetForegroundWindow不工作

C# SetForegroundWindow不工作
EN

Stack Overflow用户
提问于 2015-03-25 15:37:02
回答 2查看 8K关注 0票数 2

我已经将我的C# windows应用程序限制为一次只允许一个实例运行,使用这个问题How to force C# .net app to run only one instance in Windows?

它工作良好,不允许应用程序的多个实例同时运行。

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

我所提出的问题似乎是为了解决这个问题,但它并不适用于我。

我认为这是因为我的应用程序不符合允许方法:SetForegroundWindow工作的条件。

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

代码语言:javascript
复制
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 = Process.GetCurrentProcess();
                    foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                    {
                        if (process.Id != current.Id)
                        {
                            SetForegroundWindow(process.MainWindowHandle);
                            break;
                        }
                    }
                }
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-25 16:07:20

旋转您自己的单实例应用程序通常是一个错误,.NET框架已经强大地支持它,它是坚如磐石的,很难利用。并且有你要寻找的功能,一个StartupNextInstance事件,当用户再次启动你的应用程序时触发它。添加对Microsoft.VisualBasic的引用,并使Program.cs文件看起来如下所示:

代码语言:javascript
复制
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace WhatEverYouUse {
    class Program : WindowsFormsApplicationBase {
        [STAThread]
        static void Main(string[] args) {
            Application.SetCompatibleTextRenderingDefault(false);
            new Program().Start(args);
        }
        void Start(string[] args) {
            this.EnableVisualStyles = true;
            this.IsSingleInstance = true;
            this.MainForm = new Form1();
            this.Run(args);
        }
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) {
            eventArgs.BringToForeground = true;
            base.OnStartupNextInstance(eventArgs);
        }
    }
}

如果使用用于启动第二个实例的命令行参数(例如,在使用文件关联时是典型的),则在事件处理程序中使用eventArgs.CommandLine。

票数 6
EN

Stack Overflow用户

发布于 2017-11-20 13:33:37

试试这个..。

代码语言:javascript
复制
System.Threading.Tasks.Task.Factory.StartNew(() => { SetForegroundWindow(this.Handle); });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29260084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档