Skip to content

Commit 64cd2bc

Browse files
committed
support single instance
1 parent 6ff2013 commit 64cd2bc

File tree

4 files changed

+82
-18
lines changed

4 files changed

+82
-18
lines changed

src/XamlTheme/Adorners/RulerIndicatorAdorner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ protected override void OnRender(DrawingContext drawingContext)
3131
if (AdornedElement == null || _pen == null || DoubleUtil.IsZero(_length))
3232
return;
3333

34-
var screenPos = new Win32.POINT();
35-
if (Win32.GetCursorPos(ref screenPos))
34+
var screenPos = new Utils.Win32.POINT();
35+
if (Utils.Win32.GetCursorPos(ref screenPos))
3636
{
3737
var pos = AdornedElement.PointFromScreen(new Point(screenPos.X, screenPos.Y));
3838

src/XamlTheme/Behaviors/DragItemsPositionBehavior.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ private void EndDrag()
278278

279279
private void MoveChild(UIElement dragedChild)
280280
{
281-
var screenPos = new Win32.POINT();
282-
if (!Win32.GetCursorPos(ref screenPos))
281+
var screenPos = new Utils.Win32.POINT();
282+
if (!Utils.Win32.GetCursorPos(ref screenPos))
283283
return;
284284

285285
var posToPanel = AssociatedObject.PointFromScreen(new Point(screenPos.X, screenPos.Y));

src/XamlUtil/Common/Win32.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace XamlUtil.Common
5+
{
6+
public static class Win32
7+
{
8+
public const int WS_SHOWNORMAL = 1;
9+
10+
[DllImport("User32.dll")]
11+
public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow = WS_SHOWNORMAL);
12+
13+
[DllImport("User32.dll")]
14+
public static extern bool SetForegroundWindow(IntPtr hWnd);
15+
16+
[DllImport("User32.dll")]
17+
private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
18+
}
19+
}

src/XamlViewer/App.xaml.cs

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Linq;
33
using System.Collections.Generic;
44
using System.IO;
5+
using System.Diagnostics;
6+
using System.Reflection;
7+
using System.Threading;
58
using System.Threading.Tasks;
69
using System.Windows;
710
using System.Windows.Controls;
@@ -31,6 +34,7 @@ namespace XamlViewer
3134
public partial class App : PrismApplication
3235
{
3336
private string[] _xamlFiles = null;
37+
private Mutex _singleMutex = null;
3438

3539
public App()
3640
: base()
@@ -70,17 +74,58 @@ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionE
7074

7175
#endregion
7276

77+
public static Process GetRunningInstance()
78+
{
79+
var current = Process.GetCurrentProcess();
80+
var processes = Process.GetProcessesByName(current.ProcessName);
81+
82+
foreach (Process process in processes)
83+
{
84+
if (process.Id != current.Id)
85+
{
86+
var processLocation = Assembly.GetExecutingAssembly().Location.Replace("/", "//");
87+
88+
if (Path.GetDirectoryName(processLocation) + "\\" + Path.GetFileNameWithoutExtension(processLocation)
89+
== Path.GetDirectoryName(current.MainModule.FileName) + "\\" + Path.GetFileNameWithoutExtension(current.MainModule.FileName))
90+
return process;
91+
}
92+
}
93+
94+
return null;
95+
}
96+
7397
protected override void OnStartup(StartupEventArgs e)
74-
{
75-
if(e.Args.Length > 0)
98+
{
99+
_singleMutex = new Mutex(true, "huangjia2107_XAML_VIEWER", out bool isNew);
100+
var process = GetRunningInstance();
101+
102+
if (!isNew && process != null)
103+
{
104+
Win32.ShowWindowAsync(process.MainWindowHandle);
105+
Win32.SetForegroundWindow(process.MainWindowHandle);
106+
107+
if (e.Args.Length > 0)
108+
{
109+
var xamls = e.Args.Where(f => Path.GetExtension(f).ToLower() == ".xaml").ToArray();
110+
if (xamls != null && xamls.Length > 0)
111+
{
112+
//TODO:Send Message...
113+
}
114+
}
115+
116+
Environment.Exit(0);
117+
return;
118+
}
119+
120+
if (e.Args.Length > 0)
76121
{
77122
_xamlFiles = e.Args.Where(f => Path.GetExtension(f).ToLower() == ".xaml").ToArray();
78123
if (_xamlFiles == null || _xamlFiles.Length == 0)
79124
{
80125
Environment.Exit(0);
81126
return;
82127
}
83-
}
128+
}
84129

85130
base.OnStartup(e);
86131
}
@@ -153,10 +198,10 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry)
153198
appData.Config.Files.RemoveAll(f => _xamlFiles.Any(xf => Path.GetFullPath(xf).ToLower() == Path.GetFullPath(f).ToLower()));
154199
appData.Config.Files.InsertRange(0, _xamlFiles);
155200
}
156-
157-
//Data Source
158-
if(FileHelper.Exists(ResourcesMap.LocationDic[Location.DataSourceFile]))
159-
appData.Config.DataSourceJsonString = FileHelper.LoadFromFile(ResourcesMap.LocationDic[Location.DataSourceFile]);
201+
202+
//Data Source
203+
if (FileHelper.Exists(ResourcesMap.LocationDic[Location.DataSourceFile]))
204+
appData.Config.DataSourceJsonString = FileHelper.LoadFromFile(ResourcesMap.LocationDic[Location.DataSourceFile]);
160205

161206
containerRegistry.RegisterInstance(appData);
162207
}
@@ -167,16 +212,16 @@ protected override IModuleCatalog CreateModuleCatalog()
167212
}
168213

169214
protected override void OnInitialized()
170-
{
215+
{
171216
base.OnInitialized();
172-
217+
173218
var eventAggregator = Container.Resolve<IEventAggregator>();
174-
var appData = Container.Resolve<AppData>();
175-
219+
var appData = Container.Resolve<AppData>();
220+
176221
eventAggregator?.GetEvent<InitWorkAreaEvent>().Publish();
177-
178-
if(appData.Config.IsSyncDataSource)
179-
eventAggregator?.GetEvent<SyncDataSourceEvent>().Publish(appData.Config.DataSourceJsonString?.Trim());
222+
223+
if (appData.Config.IsSyncDataSource)
224+
eventAggregator?.GetEvent<SyncDataSourceEvent>().Publish(appData.Config.DataSourceJsonString?.Trim());
180225
}
181226
}
182227
}

0 commit comments

Comments
 (0)