大家好
最近遇到需要防止程式重複執行的需求
但是我對於C# 與 WPF 還不夠熟悉
但是還是有找到方法
給大家參考看看囉
新增一個 Program.cs 裡面寫
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace TumorReport
{
static class Program
{
/// <summary>
/// 應用程式的主要進入點。
/// </summary>
[STAThread]
static void Main()
{
Mutex mutex = new System.Threading.Mutex(true, "OnlyRun");
if (mutex.WaitOne(0, false))
{
MainWindow LoginWD = new MainWindow();
LoginWD.ShowDialog(); //呼叫主頁面
}
else
{
MessageBox.Show("程式已經在執行!", "提示");
}
}
}
}
然後將程式的起始設定成 Program.cs
基本上這樣就可以...![]()
原本最初的WPF 的程式還是不變
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 TumorReport
{
/// <summary>
/// MainWindow.xaml 的互動邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
給大家參考囉

留言板
歡迎留下建議與分享!希望一起交流!感恩!