开发者

Differences in ansychronous VB.NET and C#?

So I've been posting this week for help with an API that has asynchronous calls. You can view the CODE here: 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 Cl开发者_StackOverflow社区ientAppServer(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


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();
    }
}


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;
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜