views:

112

answers:

3

So I've been posting this week for help with an API that has asynchronous calls. You can view the CODE here: http://stackoverflow.com/questions/2638920/c-asynchronous-event-procedure-does-not-fire

With a little more digging, I found out that the API is written in VB.NET and I created a VB.NET example and guess what . . . the asynchronous calls work like a charm.

So, now I need to find out why the calls are not firing in the C# code I have. The API being written in VB really shouldn't matter, but again, the VB.NET code works and my C# does not. Is there a problem with the event handler and hows its being declared that causes it to not fire?

UPDATE VB Code added

Imports ClientSocketServices
Imports DHS_Commands
Imports DHS
Imports Utility
Imports SocketServices

Class Window1

    Public WithEvents AppServer As New ClientAppServer
    Public Token As LoginToken

    Private Sub login()

        Dim handler As New LoginHandler
        Token = handler.RequestLogin("admin", "admin", localPort:=12000, serverAddress:="127.0.0.1", serverLoginPort:=11000, clienttype:=LoginToken.eClientType.Client_Admin, timeoutInSeconds:=20)

        If Token.Authenticated Then
            AppServer = New ClientAppServer(Token, True)
            AppServer.RetrieveCollection(GetType(Gateways))
        End If

    End Sub

    Private Sub ReceiveMessage(ByVal rr As RemoteRequest) Handles AppServer.ReceiveRequest

        If TypeOf (rr.TransferObject) Is Gateways Then
            MsgBox("dd")
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        login()
    End Sub
End Class
A: 

Could you post your VB.net code here so that we can compare to your c# code?

csauve
Good point. Ive added the code to my original post
Jim Beam
This 'answer' should really be a comment to the OP's question.
Walter
+4  A: 

You're VB code is quite different from your c# code.
Try converting your vb code to c#, but keep in mind that you don't get WithEvents, so any time you
   AppServer = New ClientAppServer(Token, True);
you will need to
   AppServer.ReceiveRequest += ReceiveMessage;


EDIT: I converted your code using http://www.developerfusion.com/tools/convert/vb-to-csharp/ I also editted it by hand to make the events work. Keep in mind that I haven't tested this; I apologize for any typos/etc.

I'm also wondering, is your button click actually happening? Where is the event handler added check that it actually is in your IntializeComponent?


using DHS;
using Utility;
using SocketServices;

class Window1
{
    
    public ClientAppServer AppServer = new ClientAppServer();
    public LoginToken Token;
    

    public Window1()
    {
      InitializeComponent();
    }

    private void login()
    {
        
        LoginHandler handler = new LoginHandler();
        Token = handler.RequestLogin("admin", "admin", localPort = 12000, serverAddress = "127.0.0.1", serverLoginPort = 11000, clienttype = LoginToken.eClientType.Client_Admin, timeoutInSeconds = 20);
        
        if (Token.Authenticated) {
            AppServer = new ClientAppServer(Token, true);
            AppServer.ReceiveRequest += ReceiveMessage;
            AppServer.RetrieveCollection(typeof(Gateways));
            
        }
    }
    
    private void ReceiveMessage(RemoteRequest rr)
    {
        
        if ((rr.TransferObject) is Gateways) {
            MessageBox.Show("dd");
            
        }
    }
    
    private void Button1_Click(System.Object sender, System.Windows.RoutedEventArgs e)
    {
        login();
    }
}
csauve
First off, thanks for the conversion. I have used other converters and the code is very similar. But, I went ahead and tried yours and it still does not work. Keep in mind that all other parts of my code work - the click, the login, etc. The only part that fails is that the event procedure never fires after the retrieval command. Again, works just fine in VB, only fails with C#. Frustrating.
Jim Beam
That is quite frustrating - perhaps I can offer this:http://reflector.red-gate.com/download.aspxIt's Redgate's Reflector (formerly Lutz Roeder Reflector), it lets you decompile the IL from your assembly back into c# or vb.net.How about we try compiling your vb, then reflect it back into c# and see what happens?? I've seen the vb.net compiler do some very wonky stuff. The vb compiler team generally takes the approach of trying to "read the programmers mind", whereas the c# compiler team is quite unforgiving and does exactly what you tell it to.
csauve
Thanks for the insight. I'm going through it right now and I don't see anything that jumps out at me as far as doing things differently. At this point, I'll have to wait for support on the API.I'll keep going trough with the reflector and post back if anything does change. I've implemented 4 things that looked differently with it but the result has not improved.Many thanks for your thorough help.
Jim Beam
IT WORKS! Wow - I can't believe it's actually working. I ran through a few more items with that reflector and the way the AppServer object is being created was the key. I'll post the code in another answer, but it's finally on the right track!
Jim Beam
+1 for using the DeveloperFusion converter. That place has really helped me more times than I can remember.
Dillie-O
Actually, it was the red-gate reflector that really got me on the right track. I tried a bunch of other converters out there and none came remotely close to what the solution should be.
Jim Beam
+1  A: 

Many thanks to Collin Sauve for putting me on the right track with the reflector. The key was in how the AppServer object was being created. The working code to make that happen is below.

 public virtual ClientAppServer AppServer
    {

        get
        {
            return this._AppServer;
        }

        set
        {
            ClientAppServer.ReceiveRequestEventHandler handler = new ClientAppServer.ReceiveRequestEventHandler(this.ReceiveMessage);
            if (this._AppServer != null)
            {
                this._AppServer.ReceiveRequest -= handler;
            }
            this._AppServer = value;
            if (this._AppServer != null)
            {
                this._AppServer.ReceiveRequest += handler;
            }
        }
    }
Jim Beam