首页 > 其他分享 >WPF mutex single instance

WPF mutex single instance

时间:2024-09-30 20:22:45浏览次数:1  
标签:Mutex isCreateNew appName Windows System instance mutex using WPF

private static Mutex mtx = null;
public MainWindow()
{
    bool isCreateNew;
    string appName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    mtx = new Mutex(true, appName, out isCreateNew);
    if(!isCreateNew)
    {
        MessageBox.Show($"The app {appName} is running!", "Single Instance", MessageBoxButton.OK);
        this.Close();
    }
    InitializeComponent();
}

 

 

 

 

//

//xaml
<Window x:Class="WpfApp430.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp430"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBlock HorizontalAlignment="Stretch"
                   VerticalAlignment="Stretch"
                   Text="Single Instance"
                   FontSize="100"/>
    </Grid>
</Window>



//.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp430
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static Mutex mtx = null;
        public MainWindow()
        {
            bool isCreateNew;
            string appName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            mtx = new Mutex(true, appName, out isCreateNew);
            if(!isCreateNew)
            {
                MessageBox.Show($"The app {appName} is running!", "Single Instance", MessageBoxButton.OK);
                this.Close();
            }
            InitializeComponent();
        }
    }
}

 

标签:Mutex,isCreateNew,appName,Windows,System,instance,mutex,using,WPF
From: https://www.cnblogs.com/Fred1987/p/18442402

相关文章

  • WPF Calendar DisplayMode SelectionMode FirstDayOfWeek Start End BlackoutDates
    //xaml<Windowx:Class="WpfApp427.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF slider IsSelectionRangeEnabled TickFrequency
    <Windowx:Class="WpfApp426.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • wpf 找不到资源 *.xaml 字典文件异常处理
    找不到字典时设置如下<ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionarySource="/Themes/1.xaml"/><ResourceDictionarySource="/Themes/2.xaml"......
  • WPF MVVM入门系列教程(二、依赖属性)
    说明:本文是介绍WPF中的依赖属性功能,如果对依赖属性已经有了解了,可以浏览后面的文章。 为什么要介绍依赖属性在WPF的数据绑定中,密不可分的就是依赖属性。而MVVM又是跟数据绑定紧密相连的,所以在学习MVVM之前,很有必须先学习一下依赖属性。 依赖属性(DepencencyProperty)是什......
  • WPF Progrss bar stringformat {} {0}% IsDetermined
    //xaml<Windowx:Class="WpfApp425.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • (二)WPF布局
    <Windowx:Class="WpfTest.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.c......
  • WPF下使用FreeRedis操作RedisStream实现简单的消息队列
    RedisStream简介RedisStream是随着5.0版本发布的一种新的Redis数据类型:高效消费者组:允许多个消费者组从同一数据流的不同部分消费数据,每个消费者组都能独立地处理消息,这样可以并行处理和提高效率。阻塞操作:消费者可以设置阻塞操作,这样它们会在流中有新数据添加时被唤醒并开始......
  • .NET|--WPF|--笔记合集|--依赖项属性|--5.附加属性
    前言附加属性是一个ExtensibleApplicationMarkupLanguage(XAML)概念。附加属性允许为派生自DependencyObject的任何XAML元素设置额外的属性/值对,即使该元素未在其对象模型中定义这些额外的属性。额外的属性可进行全局访问。附加属性通常定义为没有常规属性包装......
  • WPF中播放音频文件
    SoundPlayer第一种方式,就是使用SoundPlayer。优点:平台自带,使用非常简单。缺点:只支持WAV音频格式,不支持MP3格式。示例代码:SoundPlayerplayer=newSoundPlayer("BLOW.WAV");player.Play();NAudio.NET平台,音频相关的开发,经常会用到NAudio这个库。优点:用起来相对也比较简......
  • WPF 基础 2D 图形学知识 判断点是否在线段上
    在知道一个使用两个点表示的线段,和另一个点,求另一个点是否在线段上本文算法属于通用的算法,可以在WPF和UWP和Xamarin等上运行,基本上所有的.NET平台都能执行如下图,如果点在线段上,那么修改线段颜色假定有线段的定义如下publicrecordLine{publicPo......