C#SetForeGroundInow不工作
我用这个问题限制了我的C#windows应用程序一次只允许运行一个实例 它运行良好,不允许同时运行多个应用程序实例 问题是,如果用户试图打开应用程序的第二个实例,我希望当前活动的实例出现在前面 我提出的这个问题似乎解决了这个问题,但它对我不起作用 我认为这是因为我的应用程序不符合允许该方法的标准:C#SetForeGroundInow不工作,c#,C#,我用这个问题限制了我的C#windows应用程序一次只允许运行一个实例 它运行良好,不允许同时运行多个应用程序实例 问题是,如果用户试图打开应用程序的第二个实例,我希望当前活动的实例出现在前面 我提出的这个问题似乎解决了这个问题,但它对我不起作用 我认为这是因为我的应用程序不符合允许该方法的标准:setforegroundindow 工作 我的问题是,我如何才能做到这一点。我的代码如下: using System ; using System.Collections.Generic; us
setforegroundindow
工作
我的问题是,我如何才能做到这一点。我的代码如下:
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;
}
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Windows.Forms;
使用系统线程;
使用系统诊断;
使用System.Runtime.InteropServices;
命名空间RTRFIDListener\u客户端
{
静态类程序
{
[DllImport(“user32.dll”)]
[返回:Marshallas(UnmanagedType.Bool)]
静态外部bool setforegroundindow(IntPtr hWnd);
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
bool createdNew=true;
使用(Mutex Mutex=new Mutex(true,“RTRFIDListener_Client”,out createdNew))
{
如果(createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(新的frm_Main());
}
其他的
{
Process current=Process.GetCurrentProcess();
foreach(Process.getProcessByName(current.ProcessName))中的进程
{
if(process.Id!=current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
打破
}
}
}
}
}
}
}
旋转您自己的单实例应用程序通常是一个错误,.NET Framework已经对其提供了强大的支持,而且它坚如磐石,很难利用。并且具有您正在寻找的功能,即当用户再次启动您的应用程序时触发的StartupNextInstance事件。添加对Microsoft.VisualBasic的引用,并使Program.cs文件如下所示:
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。旋转您自己的单实例应用程序通常是一个错误,.NET Framework已经对其提供了强大的支持,而且它是坚如磐石的,很难利用。并且具有您正在寻找的功能,即当用户再次启动您的应用程序时触发的StartupNextInstance事件。添加对Microsoft.VisualBasic的引用,并使Program.cs文件如下所示:
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。试试这个
System.Threading.Tasks.Task.Factory.StartNew(() => { SetForegroundWindow(this.Handle); });
试试这个
System.Threading.Tasks.Task.Factory.StartNew(() => { SetForegroundWindow(this.Handle); });
标记的副本中的答案解决了这个问题(通过广播消息),它适用于我。我看不出有什么问题。@AlexK。如果应用程序已最小化且仍在运行,则它可以正常工作,但如果它已关闭且仍在后台运行,则不会正常工作work@VanoMaisuradze请参见上面的注释:如果上一个实例的主窗口已关闭,则没有任何东西可带到前台。不是吗?标记的重复地址中的答案(通过广播消息)对我有效。我看不出有什么问题。@AlexK。如果应用程序已最小化且仍在运行,则它可以正常工作,但如果它已关闭且仍在后台运行,则不会正常工作work@VanoMaisuradze请参见上面的注释:如果上一个实例的主窗口已关闭,则没有任何东西可带到前台。不是吗?我得到了错误类型或名称空间“Microsoft.VisualBasic.ApplicationServices;”如果不存在,请仔细按照说明操作,您忘记添加引用。@HansPassant是否最好将
eventArgs.BringToForeground=true在OnStartupNextInstance
中的code>?也可以,但是接下来调用base.StartupNextInstance()。我会这样写的。我会得到错误类型或名称空间“Microsoft.VisualBasic.ApplicationServices;”如果不存在,请仔细按照说明操作,您忘记添加引用。@HansPassant是否最好将eventArgs.BringToForeground=true在OnStartupNextInstance
中的code>?也可以,但是接下来调用base.StartupNextInstance()。我会这样写的。请在你的答案中添加一些解释,说明为什么以及如何解决这个问题。只有代码的答案不适合这样做。此答案可能会被删除。请在答案中添加一些上下文。只有代码的回答不被认为是好的风格。为什么这可以解决问题?请在回答中添加一些解释,说明为什么以及如何解决问题。只有代码的答案不适合这样做。此答案可能会被删除。请在答案中添加一些上下文。只有代码的回答不被认为是好的风格。为什么这能解决问题?