@@ -7,32 +7,47 @@ Class Application
7
7
8
8
Public Shared ReadOnly mutex As New Mutex( False , "Global\CompactGUI" )
9
9
10
+ Private pipeServerCancellation As New CancellationTokenSource()
11
+ Private pipeServerTask As Task
12
+
10
13
Private mainWindow As MainWindow
11
14
12
15
Private Async Sub Application_Startup(sender As Object , e As StartupEventArgs)
13
16
14
17
SettingsHandler.InitialiseSettings()
15
18
19
+ Dim acquiredMutex As Boolean
20
+
21
+ Try
22
+ acquiredMutex = mutex.WaitOne( 0 , False )
23
+ Catch ex As AbandonedMutexException
24
+ ' This means the mutex was acquired successfully,
25
+ ' but its last owner exited abruptly, without releasing it.
26
+ ' acquiredMutex should still be True here, but further error checking
27
+ ' on shared program state could be added here as well.
28
+ acquiredMutex = True
29
+ End Try
16
30
17
- If Not SettingsHandler.AppSettings.AllowMultiInstance AndAlso Not mutex.WaitOne( 0 , False ) Then
31
+ If Not SettingsHandler.AppSettings.AllowMultiInstance AndAlso Not acquiredMutex Then
18
32
19
33
If e.Args.Length <> 0 AndAlso e.Args( 0 ) = "-tray" Then
20
34
MessageBox.Show( "An instance of CompactGUI is already running" )
21
35
Application.Current.Shutdown()
22
36
23
37
End If
24
38
25
- Using client = New NamedPipeClientStream( "CompactGUI" )
39
+ Using client = New NamedPipeClientStream( "." , " CompactGUI", PipeDirection.Out )
26
40
client.Connect()
27
41
Using writer = New StreamWriter(client)
28
- writer.WriteLine(e.Args( 0 ))
42
+ If e.Args.Length > 0 Then
43
+ writer.WriteLine(e.Args( 0 ))
44
+ End If
29
45
End Using
30
46
End Using
31
47
32
48
Application.Current.Shutdown()
33
49
ElseIf Not SettingsHandler.AppSettings.AllowMultiInstance Then
34
-
35
- ProcessNextInstanceMessage()
50
+ pipeServerTask = ProcessNextInstanceMessage()
36
51
End If
37
52
38
53
@@ -56,31 +71,40 @@ Class Application
56
71
' can be handled in this file.
57
72
58
73
59
- Private Async Sub ProcessNextInstanceMessage()
60
-
61
- Await Task.Run( Sub ()
62
- While True
63
-
64
- Using server = New NamedPipeServerStream( "CompactGUI" )
65
- server.WaitForConnection()
66
- Using reader = New StreamReader(server)
67
- Dim message = reader.ReadLine()
68
- mainWindow.Dispatcher.Invoke( Sub ()
69
- mainWindow.Show()
70
- mainWindow.WindowState = WindowState.Normal
71
- mainWindow.Topmost = True
72
- mainWindow.Activate()
73
- mainWindow.Topmost = False
74
- If message IsNot Nothing Then
75
- mainWindow.ViewModel.SelectFolder(message)
76
-
77
- End If
78
- End Sub )
79
-
80
- End Using
81
- End Using
82
- End While
83
- End Sub )
84
- End Sub
74
+ Private Async Function ProcessNextInstanceMessage() As Task
75
+ Using server = New NamedPipeServerStream( "CompactGUI" ,
76
+ PipeDirection.In,
77
+ 1 ,
78
+ PipeTransmissionMode.Byte,
79
+ PipeOptions.Asynchronous)
80
+ While Not pipeServerCancellation.IsCancellationRequested
81
+ Try
82
+ Await server.WaitForConnectionAsync(pipeServerCancellation.Token)
83
+ Catch ex As OperationCanceledException
84
+ Return
85
+ End Try
86
+ Using reader = New StreamReader(server)
87
+ Dim message = Await reader.ReadLineAsync()
88
+ mainWindow.Dispatcher.Invoke( Sub ()
89
+ mainWindow.Show()
90
+ mainWindow.WindowState = WindowState.Normal
91
+ mainWindow.Topmost = True
92
+ mainWindow.Activate()
93
+ mainWindow.Topmost = False
94
+ If message IsNot Nothing Then
95
+ mainWindow.ViewModel.SelectFolder(message)
96
+ End If
97
+ End Sub )
98
+ End Using
99
+ End While
100
+ End Using
101
+ End Function
102
+
103
+ Public Async Function ShutdownPipeServer() As Task
104
+ If pipeServerTask IsNot Nothing Then
105
+ pipeServerCancellation.Cancel()
106
+ Await pipeServerTask
107
+ End If
108
+ End Function
85
109
86
110
End Class
0 commit comments