2011-11-10 64 views
0

Salve!我想使用命令行參數退出我的GUI應用程序(vb.net 4)。我應該從命令行發送:如何通過命令行將命令發送到正在運行的應用程序

myapplication.exe quit 

- 並且已經運行的應用程序實例應該退出。 現在,我有一個互斥檢測,以便我一次只能運行一個應用程序實例。看起來,如果我發送一個命令行,它將不能在已經運行的應用程序上運行;它只適用於正在啓動的一個。

回答

1

輕鬆發送消息到運行實例的應用程序

大家好!找到這個不錯的文章here後,尋找一種方法,即單個應用程序可以對第一個實例執行一件事,但第二個實例會導致第一個實例執行其他操作。

這樣做的價值在於,您可以(使用您自己的附加程序設計)使用它來實現將命令行參數發送到正在運行的應用程序的效果。您實際上會使用應用程序的第二個實例將命令行參數作爲消息發送到第一個實例。它在我的Windows XP上可以正常工作,但我對vb.net相當陌生,所以如果您有任何改進,我想知道!

所以我改編anoriginalidea的很好的例子,下面是我的結果。這是一切的基礎,所以你只是在這裏看到這個過程的要點。

它的工作原理是這樣的:

  1. 首先,堅持這這裏模塊在應用程序中。
  2. 然後啓動應用程序1
  3. 您將收到一條消息:「InterCom Server Reporting for Duty!」
  4. 現在啓動app2 - 你會得到一條消息(它實際上是從app1!),說:「我的名字是 真的是鮑勃!」讓你知道該功能被解僱了。
  5. 然後app2將退出(您告訴它在sub main中)和 然後app1將退出,因爲當調用InterComClient() 時,app2告訴它退出。

第一添加爲您的子主 互斥將檢查,看看你的應用程序已經運行或沒有。

Public Sub Main() 
    Dim createdNew As Boolean = True 
    Using mutex As New Mutex(True, "TestForKalatorMutexProcess", createdNew) 
     If createdNew Then 
      InterComServer() 
      'BE SURE TO CHANGE myApplication TO YOUR PRIMARY FORM! 
      Application.Run(new myApplication) 
     Else 
      InterComClient() 
      application.exit() 
     End If 
    End Using 
End Sub 

現在模塊在應用程序中添加這種

Imports System.Runtime.Remoting 
Imports System.Runtime.Remoting.Channels 
Imports System.Runtime.Remoting.Channels.ipc 'You have to add this as a reference! 

Public Module intercom 
#Region "-------------InterShared-------------------------------------" 
Public Interface ICommunicationService 
    Sub SaySomething(ByVal text As String) 
End Interface 
#End Region 

#Region "-------------InterComClient-------------------------------------" 
'This will run on the second instance 
Public Sub InterComClient(ByRef intercommessage As String) 
    Try 
     Dim ipcCh As New IpcChannel("myClient") 
     ChannelServices.RegisterChannel(ipcCh, False) 
     Dim obj As ICommunicationService = DirectCast(Activator.GetObject(GetType(ICommunicationService), "ipc://IPChannelName/SreeniRemoteObj"), ICommunicationService) 
     obj.SaySomething(intercommessage) 
     Thread.Sleep(1000) 
     ChannelServices.UnregisterChannel(ipcCh) 
    Catch ex As Exception 
     'If you use this as a way to exit your application, be sure to discard this exception 
     'because the InterCom Server won't be running to receive the closing of the channel 
     'and it will throw a "Pipe Ended" error that can't be solved. 
     'To "discard" the error, simply catch it and don't do anything. 
     'Dim errmsg As New Messenger("Exception in InterComClient" & vbCr & ex.Message) 
    End Try 
End Sub 
#End Region 

#Region "-------------InterComServer-------------------------------------" 
Public Class CommunicationService 
    Inherits MarshalByRefObject 
    Implements ICommunicationService 
    Public Sub SaySomething(ByVal whatmessage As String) Implements ICommunicationService.SaySomething 
     msgbox("InterCom Client Heard this : " & whatmessage) 
     Application.exit 
    End Sub 
End Class 

Public Sub InterComServer() 
    Dim ipcCh As IpcChannel 
    ipcCh = New IpcChannel("IPChannelName") 
    ChannelServices.RegisterChannel(ipcCh, False) 
    RemotingConfiguration.RegisterWellKnownServiceType(GetType(CommunicationService), "SreeniRemoteObj", WellKnownObjectMode.Singleton) 
    MsgBox("InterCom Server Reporting for Duty!") 
End Sub 
#End Region 

End Module 

[更新]

僅供參考,任何人,我在我的應用程序現在用這個一段時間,它似乎工作得很好。

+0

不再適用於vb.net 4.5 – stigzler

+0

通常,如果它在一個dotnet版本中工作,它可以在另一個版本中工作;如果您需要向後兼容,它取決於您的代碼設置的版本。它編譯後應該可以工作。 – bgmCoder

0

如果您保持命令行打開輸入並只聽按下Enter/Return鍵,該怎麼辦?然後你可以檢查他們是否傳遞了單詞quit或其他程序命令。

+0

我不知道它可以保持開放。我怎樣才能做到這一點?我不希望任何cmd盒子掛在身邊,因爲這主要是一個GUI應用程序。我希望能夠使用另一個程序來發送命令行,或者從一個運行框中執行它。但它必須是隱形的。我該如何讓應用程序保持傾聽? – bgmCoder

+0

任何線索如何做到這一點? – bgmCoder

0

您需要的是設置某種形式的進程間通信。當第二個實例使用命令行參數啓動時,在它由於單實例互斥代碼而自行終止之前,它需要發信號通知第一個實例並將參數傳遞給它。

現在這樣說,如果你paramater正在處理的是以往任何時候都在只有命令行中退出,那麼你就可以代替interate在系統中正在運行的進程(見System.Diagnotics.Process)並殺死1實例。

+0

這聽起來很有趣。我知道如何在互斥代碼終止第二個實例之前「做」某件事 - 在那裏沒有問題。我計劃能夠通過除「退出」之外的其他參數,但是,否則我將只使用kill-process技術。 因此,如果啓動第一個實例,那麼當我使用參數啓動第二個實例時,而不是正常啓動,它會將其參數傳遞給第一個實例,然後退出? 那我該如何使用「進程間通訊?」 – bgmCoder

+0

好的,我發現這個線程:(http://www.codeproject.com/KB/IP/Net_Remoting.aspx)我想,也許這會做「進程間」的東西。 @tcarvin和rae,謝謝你的幫助。 – bgmCoder

+0

管道,套接字,放置文件,發送Windows消息......有許多方法可以執行IPC。遠程處理有點沉重,但肯定會起作用。 – tcarvin

相關問題