防止应用程序在c#中启动两次 [英] prevent application starting twice in c#

查看:76
本文介绍了防止应用程序在c#中启动两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要确保应用程序不应该运行两次。如果尝试它应该返回主窗体。

解决方案

最简单的方法是使用Mutex来判断另一个实例是否已在运行。网上肯定有很多关于此的文章?


可能会有所帮助,



C#中的单个实例应用程序 [ ^ ]



单个流程实例对象 [ ^ ]



单一实例C#应用程序 - 适用于.NET 2.0 [ ^ ]



:)


我在vb WPF中制作播放器,这对我有用。它会关闭其他实例。希望它能帮到你。只需转换为C#:)



  Imports  System.Threading 

私有 共享 appGuid As 字符串 = c0a76b5a-12ab-45c5-b9d9 -d693faa6e7b9 ' 您的应用独特指南
私有 共享互斥作为 Mutex
公共 Sub ()
InitializeComponent()
Dim mutexCreated As Boolean

mutex = 新的 Mutex( True Global \ + appGuid,mutexCreated)
如果 mutexCreated 那么
互斥锁.ReleaseMutex()
结束 如果

如果 mutexCreated 那么
应用已在运行,关闭此项!
环境。[退出]( 0
结束 如果
结束 Sub


Hi,

I need to ensure that application should not run twice.If attempt it should return to main form.

解决方案

The easiest way to do this is to use a Mutex to tell if another instance is already running. There's surely tons of articles online about this ?


It might be helpful,

Single Instance Application in C#[^]

Single Process Instance Object[^]

Single-Instance C# Application - for .NET 2.0[^]

:)


I was making Player in vb WPF and just this worked for me. It closes other instances. Hope it helps you. Just convert to C# :)

Imports System.Threading

Private Shared appGuid As String = "c0a76b5a-12ab-45c5-b9d9-d693faa6e7b9" 'Your App Unique Guid
    Private Shared mutex As Mutex
    Public Sub New()
        InitializeComponent()
        Dim mutexCreated As Boolean

        mutex = New Mutex(True, "Global\" + appGuid, mutexCreated)
        If mutexCreated Then
            mutex.ReleaseMutex()
        End If

        If Not mutexCreated Then
            'App is already running, close this!
            Environment.[Exit](0)
        End If
    End Sub


这篇关于防止应用程序在c#中启动两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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