@@ -7,6 +7,9 @@ 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)
@@ -33,7 +36,7 @@ Class Application
33
36
34
37
End If
35
38
36
- Using client = New NamedPipeClientStream( "CompactGUI" )
39
+ Using client = New NamedPipeClientStream( "." , " CompactGUI", PipeDirection.Out )
37
40
client.Connect()
38
41
Using writer = New StreamWriter(client)
39
42
writer.WriteLine(e.Args( 0 ))
@@ -42,8 +45,7 @@ Class Application
42
45
43
46
Application.Current.Shutdown()
44
47
ElseIf Not SettingsHandler.AppSettings.AllowMultiInstance Then
45
-
46
- ProcessNextInstanceMessage()
48
+ pipeServerTask = ProcessNextInstanceMessage()
47
49
End If
48
50
49
51
@@ -67,31 +69,40 @@ Class Application
67
69
' can be handled in this file.
68
70
69
71
70
- Private Async Sub ProcessNextInstanceMessage()
71
-
72
- Await Task.Run( Sub ()
73
- While True
74
-
75
- Using server = New NamedPipeServerStream( "CompactGUI" )
76
- server.WaitForConnection()
77
- Using reader = New StreamReader(server)
78
- Dim message = reader.ReadLine()
79
- mainWindow.Dispatcher.Invoke( Sub ()
80
- mainWindow.Show()
81
- mainWindow.WindowState = WindowState.Normal
82
- mainWindow.Topmost = True
83
- mainWindow.Activate()
84
- mainWindow.Topmost = False
85
- If message IsNot Nothing Then
86
- mainWindow.ViewModel.SelectFolder(message)
87
-
88
- End If
89
- End Sub )
90
-
91
- End Using
92
- End Using
93
- End While
94
- End Sub )
95
- End Sub
72
+ Private Async Function ProcessNextInstanceMessage() As Task
73
+ Using server = New NamedPipeServerStream( "CompactGUI" ,
74
+ PipeDirection.In,
75
+ 1 ,
76
+ PipeTransmissionMode.Byte,
77
+ PipeOptions.Asynchronous)
78
+ While Not pipeServerCancellation.IsCancellationRequested
79
+ Try
80
+ Await server.WaitForConnectionAsync(pipeServerCancellation.Token)
81
+ Catch ex As OperationCanceledException
82
+ Return
83
+ End Try
84
+ Using reader = New StreamReader(server)
85
+ Dim message = Await reader.ReadLineAsync()
86
+ mainWindow.Dispatcher.Invoke( Sub ()
87
+ mainWindow.Show()
88
+ mainWindow.WindowState = WindowState.Normal
89
+ mainWindow.Topmost = True
90
+ mainWindow.Activate()
91
+ mainWindow.Topmost = False
92
+ If message IsNot Nothing Then
93
+ mainWindow.ViewModel.SelectFolder(message)
94
+ End If
95
+ End Sub )
96
+ End Using
97
+ End While
98
+ End Using
99
+ End Function
100
+
101
+ Public Async Function ShutdownPipeServer() As Task
102
+ If pipeServerTask IsNot Nothing Then
103
+ pipeServerCancellation.Cancel()
104
+ Await pipeServerTask
105
+ End If
106
+ End Function
96
107
97
108
End Class
0 commit comments