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!
}
}
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
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.