if (InvokeRequired)
{
BeginInvoke(new MethodInvoker(delegate()
{
textBox1.Text = args.Fax.Port.ToString();
textBox2.Text = args.Fax.FaxStatus.ToString();
}));
}
views:
206answers:
6Courtesy of developerfusion.com:
If InvokeRequired Then
BeginInvoke(New MethodInvoker(Function() Do
textBox1.Text = args.Fax.Port.ToString()
textBox2.Text = args.Fax.FaxStatus.ToString()
End Function))
End If
Until Visual Studio 2010, VB.Net does not support multi-statement anonymous functions.
You need to move the anonymous method into a separate method that takes args
as a parameter, then call Invoke
on it.
VB.NET 9 and previous does not support multi line anonymous functions. You need to write a separate function:
If InvokeRequired Then
BeginInvoke(New MethodInvoker(AddressOf MySub))
End If
where MySub:
Sub MySub()
textBox1.Text = args.Fax.Port.ToString()
textBox2.Text = args.Fax.FaxStatus.ToString()
End Sub
UPDATE:
To clarify about the args
parameter there's an overload of BeginInvoke that could be used and in this case the MethodInvoker delegate is no longer suitable. Action(Of T) would work:
If InvokeRequired Then
BeginInvoke(New Action(Of SomeType)(AddressOf MySub), args)
End If
and:
Sub MySub(ByVal args as SomeType)
textBox1.Text = args.Fax.Port.ToString()
textBox2.Text = args.Fax.FaxStatus.ToString()
End Sub
After all it's simple things like this that make C# developers happy :-) (please don't take it wrong, I have nothing against VB.NET)
From MSDN
Delegate Sub MyDelegate(myControl As Label, myArg2 As String)
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim myArray(1) As Object
myArray(0) = New Label()
myArray(1) = "Enter a Value"
myTextBox.BeginInvoke(New MyDelegate(AddressOf DelegateMethod), myArray)
End Sub
Public Sub DelegateMethod(myControl As Label, myCaption As String)
myControl.Location = New Point(16, 16)
myControl.Size = New Size(80, 25)
myControl.Text = myCaption
Me.Controls.Add(myControl)
End Sub
So
if (InvokeRequired)
{
BeginInvoke(new MethodInvoker(delegate()
{
textBox1.Text = args.Fax.Port.ToString();
textBox2.Text = args.Fax.FaxStatus.ToString();
}));
}
Delegate Sub MyDelegate(faxPort As String, faxStatus As String)
If InvokeRequired Then
Dim aArray(1) as Object
aArray(0) = args.Fax.Port.ToString()
aArray(1) = args.Fax.FaxStatus.ToString();
BeginInvoke(New MyDelegate(AddressOf MySub), aArray)
End If
Sub MySub( faxPort as String, faxStatus as String)
textBox1.Text = faxPort
textBox2.Text = faxStatus
End Sub
I think
Since the comments do not allow formatting, I'm going to post my working solution here. Thanks to all who chimed in - Darin, SLaks, drachenstern, Jay.
Private Sub myControl_FaxStatus(ByVal sender As Object, ByVal args As DataTech.FaxManNet.FaxEventArgs)
frmModemStatus.UpdateStatus(args.Fax)
If InvokeRequired Then
Dim aArray(1) As Object
aArray(0) = args.Fax.Port.ToString()
aArray(1) = args.Fax.FaxStatus.ToString()
BeginInvoke(New MyDelegate(AddressOf UpdateStatusDisplay), aArray)
End If
Try
TextBox1.Text = args.Fax.Port.ToString()
TextBox2.Text = args.Fax.FaxStatus.ToString()
Catch ex As Exception
System.Diagnostics.Debug.WriteLine("ex = " & ex.ToString())
End Try
End Sub
Delegate Sub MyDelegate(ByVal faxPort As String, ByVal faxStatus As String)
Private Sub UpdateStatusDisplay(ByVal faxPort As String, ByVal faxStatus As String)
TextBox1.Text = faxPort
TextBox2.Text = faxStatus
End Sub
If you need similar stuff in the feature just install the following component to your visual studio. After that you can easly convert from c# to vb.net or vb.net to c#. VS2010 Support is already included. http://codeconvert.codeplex.com/