views:

206

answers:

6
if (InvokeRequired)
{
  BeginInvoke(new MethodInvoker(delegate()
  {
    textBox1.Text = args.Fax.Port.ToString();
    textBox2.Text = args.Fax.FaxStatus.ToString();
  }));
}
+4  A: 

Courtesy 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
Jay Riggs
Yeah, I tried that too. Didn't work. Won't compile.
beaudetious
@beaudetious: there might be some problem with other parts of your code!
Mahesh Velaga
This syntax is new to VB 10, but is slightly wrong.
SLaks
My code is working fine in the c# app I wrote. Now I'm trying to write a VB.Net version for a client and this one part is holding me up.
beaudetious
A cursory examination of the code shows it to be a 1:1 correlation. Also, given that C# and VB.NET are _nearly_ 1:1 there's not much way for it to be wrong. ~ what's the compiler error?
drachenstern
@drachenstern: Until 2010, VB.Net does not support this.
SLaks
Yes I just saw your comment. Also, the comment by Darin Dimitrov doesn't include a reference to args per your comment.
drachenstern
+3  A: 

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.

SLaks
Really? Thanks for the info. I was not aware of that one-line limitation with VB.NET <10
drachenstern
A code sample would help me out here. Thanks.
beaudetious
+2  A: 

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)

Darin Dimitrov
`args` is probably a parameter to the original method.
SLaks
Great suggestion, but how do I pass the arguments to MySub from MethodInvoker?
beaudetious
@beaudetious, please see my update.
Darin Dimitrov
+3  A: 

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

drachenstern
and of course I left out a few bits /facepalm ... at least I copied the sample and gave the link ... >.>
drachenstern
A: 

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
beaudetious
A: 

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/

Kerem Kusmezer