vb.net 2008中只有一个Form实例 [英] Only one instance of form in vb.net 2008

查看:80
本文介绍了vb.net 2008中只有一个Form实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想打开一个表单实例.如果我关闭该表单,还需要再次打开该表单的一个实例.

解决方案

如果您要谈论的是仅打开一个APP实例,则您可以按照其他答案的建议进行操作.

如果您仅在谈论打开一个FORM实例,那么您只需保留一个布尔值标记即可跟踪它.创建表单时将其设置为True,关闭表单时将其设置为False.如果尝试打开另一个实例,请首先检查该标志的值.


尝试:
VB.NET中的单实例应用程序 [ http://www.vb-helper.com/howto_net_one_instance.html [ http://blogs.lessthandot.com/index. php/DesktopDev/MSTech/vb-net-single-instance-application-the-b [ // 为您的应用设置唯一的名称 静态 Mutex互斥体= Mutex( false " ); /// /// 应用程序的主要入口点. /// [STAThread] 静态 无效 Main() { // 延迟3秒,以确保没有其他实例在运行 如果(!mutex.WaitOne(TimeSpan.FromSeconds( 3 )," ); 返回; } 尝试 { // 没有其他实例在运行 // 启动应用程序 Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault( false ); Application.Run( Form1()); } 最终 { Mutex.ReleaseMutex(); } }


来源:
如何强制在C#中仅强制运行应用程序的一个实例


i want to open only one instance of form. And more if i close the form then again i have to open the one instance of form.

解决方案

If you''re talking about opening only one instance of your APP, then you follow the advice of the other answers.

If you''re talking about opening only a single instance of a FORM, then you simply keep a boolean flag to track that. Set it to True when you create the form and False when the form is closed. If you try to open another instance, check the value of the flag first.


Try:
Single Instance Application in VB.NET[^]
http://www.vb-helper.com/howto_net_one_instance.html[^]
http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/vb-net-single-instance-application-the-b[^]


this how i''ve done it :

// Setup a unique Name for your Application
static Mutex mutex = new Mutex(false, "My_Unique_App_ID");

///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
    // Delay 3 seconds to be sure that there is no other instance running
    if (!mutex.WaitOne(TimeSpan.FromSeconds(3), false))
    {
        MessageBox.Show("Another instance of this application is already running");
        return;
    }

    try
    {
        // There is no other instances running
        // Launch the application
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    finally
    {
        mutex.ReleaseMutex();
    }
}


Source : How to force running only one instance of your application in C#


这篇关于vb.net 2008中只有一个Form实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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