2

I have a requirement to implement a single VB.NET instance of an application on a terminal server. To do this I am using code from the Flawless Code blog. It works well, except in the code is written in C# and uses an anonymous method which is not supported in VB.NET. I need to rewrite the following so that I can use it as an event in 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!
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Skittles
  • 897
  • 1
  • 15
  • 24

3 Answers3

6

You can use this code:

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
Tereza Tomcova
  • 4,928
  • 4
  • 30
  • 29
  • Hi Ondatra, thanks for the response, this looks like it could work however, when I try to implement it, I get a ''AddressOf' expression cannot be converted to 'System.Delegate' because type 'System.Delegate' is declared 'MustInherit' and cannot be created.' error. Again thank you for your help. – Skittles Aug 02 '09 at 23:57
  • Thanks Ondatra. That seems to work. Except now I keep getting that the form Is Nothing. I will work on that and let you know what I do to fix it. Once again, thanks to everyone who helped. I really appreciate it. – Skittles Aug 03 '09 at 00:43
  • Hi Ondatra, I figured out the form issue but now I am getting a 'System.Reflection.TargetParameterCountException was unhandled Message="Parameter count mismatch." Source="System.Windows.Forms"' error. Any ideas? Hate to keep asking but just feel that I am so close to an answer now with all the help on here. Thanks again. – Skittles Aug 03 '09 at 01:10
  • I got it (I think). It was thanks to a combination of both Ondatra and kek444's answers. I had to change the line, form.Invoke(New Action(Of String())(AddressOf updateFormMethod), e.Args) to form.Invoke(New Action(Of String())(AddressOf updateFormMethod), New Object() {e.Args}) Thanks again for all the help.... – Skittles Aug 03 '09 at 01:33
4

In VB.NET in VS 2010 you can do the following:

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
Paul van Brenk
  • 7,450
  • 2
  • 33
  • 38
  • Hi pb, thanks, I am using VS2008 however, it doesn't seem to like me declaring a Sub within a Sub. I get 'End Sub' must be preceded by a matching 'Sub'.. – Skittles Aug 03 '09 at 00:45
3

This:

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

Would be the same as:

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

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

Then you do the VB.net transition easily, to something like this:

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
Kenan E. K.
  • 13,955
  • 3
  • 43
  • 48
  • Thanks kek444, I think that I got it thanks you a combination of your answer and ondatra's answer.... – Skittles Aug 03 '09 at 01:35