2

ターミナル サーバーにアプリケーションの単一の VB.NET インスタンスを実装する必要があります。これを行うために、Flawless Codeブログのコードを使用しています。コードが C# で記述されていて、VB.NET でサポートされていない匿名メソッドを使用していることを除いて、これはうまく機能します。VB.NET でイベントとして使用できるように、以下を書き換える必要があります。

static Form1 form;

static void singleInstance_ArgumentsReceived(object sender, ArgumentsReceivedEventArgs e)
    {
        if (form == null)
            return;

        Action<String[]> updateForm = arguments =>
            {
                form.WindowState = FormWindowState.Normal;
                form.OpenFiles(arguments);
            };
        form.Invoke(updateForm, (Object)e.Args); //Execute our delegate on the forms thread!
    }
}
4

3 に答える 3

6

次のコードを使用できます。

Private Shared form As Form1

Private Shared Sub singleInstance_ArgumentsReceived(ByVal sender As Object, ByVal e As ArgumentsReceivedEventArgs)
    If form Is Nothing Then Return
    form.Invoke(New Action(Of String())(AddressOf updateFormMethod), e.Args)
End Sub

Private Shared Sub updateFormMethod(ByVal arguments As String())
    form.WindowState = FormWindowState.Normal
    form.OpenFiles(arguments)
End Sub
于 2009-08-02T23:41:38.647 に答える
4

VS 2010 の VB.NET では、次のことができます。

Shared form As Form1
Shared Sub singleInstance_ArgumentsReceived(ByVal sender As Object, ByVal e As ArgumentsReceivedEventArgs)
    If form Is Nothing Then Return

    Dim updateForm As Action(Of String()) = Sub(arguments)
                                                form.WindowState = FormWindowState.Normal
                                                form.OpenFiles(arguments)
                                            End Sub

    form.Invoke(updateForm, e.args)

End Sub
于 2009-08-02T23:59:43.547 に答える
3

これ:

public void Somemethod()
{
    Action<String[]> updateForm = arguments =>
        {
            form.WindowState = FormWindowState.Normal;
            form.OpenFiles(arguments);
        };
}

次と同じです。

public void Somemethod()
{
    Action<String[]> updateForm = OnAction;
}

//named method
private void OnAction(string[] arguments)
{
    form.WindowState = FormWindowState.Normal;
    form.OpenFiles(arguments);
}

次に、VB.net への移行を次のように簡単に行います。

Public Sub SomeMethod()

    Dim updateForm As Action(Of String()) = New Action(Of String())(AddressOf Me.OnAction)
    Me.form.Invoke(updateForm, New Object() { e })

End Sub

Private Sub OnAction(ByVal arguments As String())
    form.WindowState = FormWindowState.Normal
    form.OpenFiles(arguments)
End Sub
于 2009-08-02T23:38:47.920 に答える