Bläddra i källkod

允许用户运行多个实例,并且在主界面的左下角用一个Label来提示

djs 3 månader sedan
förälder
incheckning
f2a2cfecd9

+ 28 - 12
MeterVision/App.xaml.cs

@@ -4,6 +4,7 @@ using System;
 using System.Collections.Generic;
 using System.Configuration;
 using System.Data;
+using System.Diagnostics;
 using System.Linq;
 using System.Threading;
 using System.Threading.Tasks;
@@ -29,19 +30,24 @@ namespace MeterVision
         private static Mutex mutex = new Mutex(true, "{E1805A91-1078-417D-9B65-43FCFCA504CC_ttpp}");
         protected override void OnStartup(StartupEventArgs e)
         {
-            if (!mutex.WaitOne(TimeSpan.Zero, true))
-            {
-                //如果另一个实例已经拥有这个互斥锁,那么当前实列将不会继续运行
-                MessageBox.Show("程序已经在运行。");
-                Current.Shutdown();
-                return;
-            }
+            //if (!mutex.WaitOne(TimeSpan.Zero, true))
+            //{
+            //    //如果另一个实例已经拥有这个互斥锁,那么当前实列将不会继续运行
+            //    MessageBox.Show("程序已经在运行。");
+            //    Current.Shutdown();
+            //    return;
+            //}
 
             base.OnStartup(e);
+
+            int instanceIndex = GetCurrentInstanceIndex();
+            //MessageBox.Show($"当前是第 {instanceIndex + 1} 个实例");
+
             this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
 
 
             MainWindow mainWindow = new MainWindow();
+            mainWindow.InstanceIndex = instanceIndex+1;
             this.MainWindow = mainWindow;
 
             LicenseMana.mLicenseModel = new LicenseModel()
@@ -78,11 +84,11 @@ namespace MeterVision
         protected override void OnExit(ExitEventArgs e)
         {
             //释放互斥锁
-            if(mutex != null)
-            {
-                mutex.ReleaseMutex();
-                mutex.Close();
-            }
+            //if(mutex != null)
+            //{
+            //    mutex.ReleaseMutex();
+            //    mutex.Close();
+            //}
             base.OnExit(e);
         }
 
@@ -104,7 +110,17 @@ namespace MeterVision
             }
         }
 
+        private int GetCurrentInstanceIndex()
+        {
+            string processName = Process.GetCurrentProcess().ProcessName;
+            var allProcesses = Process.GetProcessesByName(processName)
+                .OrderBy(p => p.StartTime) // 按启动时间排序
+                .ToList();
 
+            int index = allProcesses.FindIndex(p => p.Id == Process.GetCurrentProcess().Id);
+            return index; // 从0开始,表示第一个、第二个...
+        }
+        ////////////////////////////////////////////////////////////////////////////////
 
     }
 }

+ 12 - 7
MeterVision/MainWindow.xaml

@@ -196,13 +196,18 @@
 
         <!--状态栏部分-->
         <Border Grid.Row="2" Background="#F0F0F0" BorderThickness="0 1 0 0" BorderBrush="#ACAAAA">
-            <StackPanel Orientation="Horizontal" Margin="5 0 5 0" VerticalAlignment="Center">
-                <!--<TextBlock Text="永久有效" FontSize="14" Background="Transparent" Foreground="Green" Margin="0 0 10 0"/>-->
-                <TextBlock Text="ONNX模型名称:" FontSize="14" Background="Transparent" Margin="0 0 10 0"/>
-                <TextBlock Text="{Binding CurConfigItem.OnnxPath}" FontSize="14" Background="Transparent" />
-
-                <TextBlock Text="AI动态库名称:" FontSize="14" Background="Transparent" Margin="20 0 10 0"/>
-                <TextBlock Text="{Binding CurConfigItem.AiDll}" FontSize="14" Background="Transparent" />
+            <StackPanel Orientation="Horizontal">
+                <Label x:Name="lblInstanceIndex" Content="第一个"  Margin="0 0 10 0" Padding="5 0 5 0"
+                       Background="Green" Foreground="White"  FontSize="14"
+                       VerticalContentAlignment="Center" HorizontalContentAlignment="Center"  />
+                <StackPanel Orientation="Horizontal" Margin="5 0 5 0" VerticalAlignment="Center">
+                    <!--<TextBlock Text="永久有效" FontSize="14" Background="Transparent" Foreground="Green" Margin="0 0 10 0"/>-->
+                    <TextBlock Text="ONNX模型名称:" FontSize="14" Background="Transparent" Margin="0 0 10 0"/>
+                    <TextBlock Text="{Binding CurConfigItem.OnnxPath}" FontSize="14" Background="Transparent" />
+
+                    <TextBlock Text="AI动态库名称:" FontSize="14" Background="Transparent" Margin="20 0 10 0"/>
+                    <TextBlock Text="{Binding CurConfigItem.AiDll}" FontSize="14" Background="Transparent" />
+                </StackPanel>
             </StackPanel>
         </Border>
 

+ 21 - 0
MeterVision/MainWindow.xaml.cs

@@ -73,6 +73,10 @@ namespace MeterVision
 
         //字典用于缓存已加载的页面
         private Dictionary<string, object> pageCache = new Dictionary<string, object>();
+
+        //第几个实例
+        public int InstanceIndex { get; set; }
+
         public MainWindow()
         {
             InitializeComponent();
@@ -120,6 +124,23 @@ namespace MeterVision
             {
                 this.Title = $"{this.Title} (未注册)";
             }
+
+            //控制lblInstanceIndex背景色的颜色
+            int colorIndex = InstanceIndex % 3;
+            if(colorIndex == 1)
+            {
+                lblInstanceIndex.Background = Brushes.Red;
+            }
+            else if (colorIndex == 2)
+            {
+                lblInstanceIndex.Background = Brushes.Green;
+            }
+            else
+            {
+                lblInstanceIndex.Background = Brushes.Blue;
+            }
+            lblInstanceIndex.Content = $"第{InstanceIndex}个";
+
         }
 
 

+ 2 - 2
MeterVision/Properties/AssemblyInfo.cs

@@ -51,5 +51,5 @@ using System.Windows;
 // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
 // 方法是按如下所示使用“*”: :
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("3.3.1.3")]
-[assembly: AssemblyFileVersion("3.3.1.3")]
+[assembly: AssemblyVersion("3.3.1.5")]
+[assembly: AssemblyFileVersion("3.3.1.5")]

+ 1 - 1
MeterVision/db/DBStand.cs

@@ -365,7 +365,7 @@ namespace MeterVision.db
                             (@StandDetailId, @CreateTime, @StandId, @SrcImage, @StandValue, 
                              @StationId, @DeviceSn, @SampleTime, @ENumCount, @ELastUnit, 
                              @MeterType, @BrightVal, @FlowRate, @DialRegion, @NumCount, 
-                             @IndCount, @FeatureRegion, @LastUnit, @LastValue, @LastTime);";
+                             @IndCount, @FeatureRegion, @LastUnit, @NumInUpper,@LastValue, @LastTime);";
 
                         foreach (var standDetail in standDetails)
                         {