多个子表单打开 [英] Multiple Sub Form Opening

查看:56
本文介绍了多个子表单打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好


在我的Program.cs中,代码是:

Hi
In my Program.cs the code is :

bool createdNew;
m_Mutex = new Mutex(true, "App", out createdNew);
if (createdNew)
     Application.Run(new StartForm());
else
     MessageBox.Show("The application is already running.", Application.ProductName,
                  MessageBoxButtons.OK, MessageBoxIcon.Exclamation);





在InitializeComponent之后的StartForm.cs中,代码是:



In my StartForm.cs after InitializeComponent the code is :

Form1 f1 = new Form1();
f1.Visible = false;
f1.ShowDialog(this);





我的要求这是:

当我在实例已经运行时将应用程序exe加倍时,而不是只显示应用程序正在运行我想检查Form1是否已经打开。

1.如果Form1已经打开,则什么都不做。

2.如果Form1没有从StartForm.cs打开showdialog Form1



请注意,StartForm始终保留在systray中



提前谢谢



My requirement here is :
when I double the application exe when an instance is already running, instead of just displaying "Application is running" I want to check if Form1 is already open.
1. If Form1 is already open do nothing.
2. If Form1 not open showdialog Form1 from StartForm.cs

Please note that StartForm always stays in systray

Thanks in advance

推荐答案

如果您正在尝试实现单实例应用程序行为,我的过去的答案可以帮助您:

仅向一个流程实例输入多个命令 [ ^ ],

自定义Windows右键单击命令启动多个实例 [ ^ ]。



欢迎您提出后续问题。



-SA
If you are trying to implement single-instance application behavior, my past answers can help you:
Enter multiple commands to only one process instance[^],
Custom Windows right-click command launching multiple instances[^].

Your follow-up questions will be welcome.

—SA


首先,你的帖子暗示(我读过)你的App可能在没有创建Form实例的情况下运行(所以消息 应用程序的泵不依赖于表单)建议Windows服务可能是适合您的App结构的正确选择。如果我错误地解释了您的帖子,请忽略此评论。



其次,如何制作单实例WinForms应用程序的问题是许多人已经解决的问题:



1. Scott Hanselman在2008年使用C#中的Visual Basic库提出了一个解决方案:[ ^ ]。



2.使用Mutex ...即,显然,在你的脑海里......请参阅CodeProject文章:C#中的单一实例表单应用程序2013年12月:[ ^ ]。



到目前为止最好的例子和讨论,我用过的单实例应用程序使用Mutex是在Mark Michaelis的Essential C#4.0第19.8章中。可以在此处下载(.pdf)该书的示例代码:[ ^ ],您将在第19章文件夹中找到他的示例代码。



3. Chris Sells在2006年出版的书中C#WinForms(第14章)介绍了运行WinForms应用程序的几种有趣的替代方法:[ ^ ]。



第14章可在此免费下载(.pdf):[ ^ ]。



请注意本章的内容不包括创建单实例应用程序;为此,Sells和Hanselman一样,使用Visual Basic库,这在另一章中有所涉及。
First, the implication (that I read) in your post that there could be your App running without a Form instance created (so the "Message Pump" of the Application does not depend upon a Form) suggests a Windows Service might be the right choice of App structure for you. Just ignore this comment if I incorrectly interpret your post.

Second, the question of how to make a single-instance WinForms App is one many people have addressed:

1. Scott Hanselman presented a solution in 2008 using the Visual Basic Library in C#: [^].

2. To use the Mutex ... which is, evidently, on your mind ... see the CodeProject article: "Single Instance Form Application in C#" December 2013: [^].

By far the best example, and discussion, of the use of Mutex for a single-instance app I have seen is in Mark Michaelis' "Essential C# 4.0," Chapter 19.8. The sample code for that book can be downloaded (.pdf) here: [^], and you will find his sample code in the Chapter 19 folder.

3. Chris Sells in his 2006 book on C# WinForms (Chapter 14) covers several interesting alternative ways to run a WinForms Application: [^].

Chapter 14 is available as a free download (.pdf) here: [^].

Note that this chapter does not cover creating a single-instance application; for that, Sells, like Hanselman, uses the Visual Basic Library, and that is covered in another chapter.


这篇关于多个子表单打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆