vb.net “制作单实例应用程序”这是做什么的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1328792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:21:41  来源:igfitidea点击:

"make single instance application" what does this do?

vb.netwinforms

提问by l--''''''---------''''''''''''

in vb 2008 express this option is available under application properties. does anyone know what is its function? does it make it so that it's impossible to open two instances at the same time?

在 vb 2008 express 中,此选项在应用程序属性下可用。有谁知道它的功能是什么?它是否使它不可能同时打开两个实例?

采纳答案by user2866752

There is even a easier method:

还有一个更简单的方法:

Use the following code...

使用以下代码...

Imports System.IO

On the main form load event do the following:

在主窗体加载事件上执行以下操作:

If File.Exist(Application.StartupPath & "\abc.txt") Then
    'You can change the extension of the file to what ever you desire ex: dll, xyz etc.
    MsgBox("Only one Instance of the application is allowed!!!")
    Environment.Exit(0)
Else
    File.Create(Application.StartupPath & "\abc.txt", 10, Fileoptions.DeleteonClose)
Endif

This will take care of single instances as well as thin clients, and the file cannot be deleted while the application is running. and on closing the application or if the application crashes the file will delete itself.

这将处理单个实例以及瘦客户端,并且在应用程序运行时无法删除文件。并在关闭应用程序或应用程序崩溃时,文件将自行删除。

回答by sepp2k

does it make it so that it's impossible to open two instances at the same time?

它是否使它不可能同时打开两个实例?

Yes.

是的。

回答by Steve

Why not just use a Mutex? This is what MS suggests and I have used it for many-a-years with no issues.

为什么不直接使用互斥锁?这就是 MS 的建议,我已经使用它很多年了,没有任何问题。

Public Class Form1
Private objMutex As System.Threading.Mutex
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Check to prevent running twice
    objMutex = New System.Threading.Mutex(False, "MyApplicationName")
    If objMutex.WaitOne(0, False) = False Then
        objMutex.Close()
        objMutex = Nothing
        MessageBox.Show("Another instance is already running!")
        End
    End If
    'If you get to this point it's frist instance

End Sub
End Class

When the form, in this case, closes, the mutex is released and you can open another. This works even if you app crashes.

在这种情况下,当窗体关闭时,互斥锁被释放,您可以打开另一个。即使您的应用程序崩溃,这也有效。

回答by MarkJ

Yes, it makes it impossibleto open two instances at the same time.

是的,无法同时打开两个实例。

However it's very importantto be aware of the bugs. With some firewalls, it's impossibleto open even oneinstance - your application crashes at startup! See this excellent articleby Bill McCarthy for more details, and a technique for restricting your application to one instance. His technique for communicating the command-line argument from a second instance back to the first instance uses pipes in .NET 3.5.

但是,了解错误非常重要。使用某些防火墙,甚至无法打开一个实例 - 您的应用程序在启动时崩溃!有关更多详细信息以及将应用程序限制为一个实例的技术,请参阅Bill McCarthy撰写这篇优秀文章。他将命令行参数从第二个实例传回第一个实例的技术使用了 .NET 3.5 中的管道。

回答by R.Alonso

    Dim _process() As Process
    _process = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)
    If _process.Length > 1 Then
        MsgBox("El programa ya está ejecutandose.", vbInformation)
        End
    End If

回答by Mojtaba Rezaeian

I found a great article for this topic: Single Instance Application in VB.NET.

我找到了一篇关于这个主题的很棒的文章:VB.NET 中的单实例应用程序

Example usage:

用法示例:

Module ModMain

    Private m_Handler As New SingleInstanceHandler()
    ' You should download codes for SingleInstaceHandler() class from:
    ' http://www.codeproject.com/Articles/3865/Single-Instance-Application-in-VB-NET

    Private m_MainForm As Form

    Public Sub Main(ByVal args() As String)
        AddHandler m_Handler.StartUpEvent, AddressOf StartUp ' Add the StartUp callback
        m_Handler.Run(args)
    End Sub

    Public Sub StartUp(ByVal sender As Object, ByVal event_args As StartUpEventArgs)
        If event_args.NewInstance Then ' This is the first instance, create the main form and addd the child forms
            m_MainForm = New Form()
            Application.Run(m_MainForm)
        Else ' This is coming from another instance
             ' Your codes and actions for next instances...
        End If
    End Sub

End Module

回答by Web

Yes you're correct in that it will only allow one instance of your application to be open at a time.

是的,您是对的,因为它一次只允许打开一个应用程序实例。