0

Together with my friend, we are making simple chat application. Client is in C# and server in Java. Entire communication is based on WebSockets.

I have problem with designing optimal application flow. Currently it's working like this:

Steps to login:

  1. Press login button which calls Send method with data as parameter
  2. Controller has subscribed event callback
  3. When response is recieved then OnLogin event is fired

Sample code:

void Login()   
{
    var packet = new LoginDataPacket() { Login = login, Password = SecurityHandler.GetShaWithSalt(password) };

    LoginEndpoint.Send(typeof(LoginDataPacket).Name, packet);
    LoginView.LoginStarted(); 
}


void OnLoginStatus(LoginStatusPacket packet)
{
    if (packet.Status)
    {
        LoginView.LoginSuccess();
    }
    else
    {
        LoginView.LoginFailed();
    }
}

And somewhere in the view:

 public void LoginFailed()
    {
        this.InvokeOnRequired(() =>
        {
            SetControlsState(true);
            labelStatus.ForeColor = Color.Red;
            labelStatus.Text = "Failed to login";
        });

    }

public void LoginSuccess()
{
    this.InvokeOnRequired(() =>
        {
            SetControlsState(true);
            labelStatus.ForeColor = Color.Green;
            labelStatus.Text = "Login OK!";
            Close();
        });
}

Everything works fine but...

I would like to know if operation was succesfully or not inside Login method. I can't do this because my network library recieves data using events and also because it would block main thread and then UI would be frozen. Question is how it should be done correctly? I read something about async/await and TaskCompletionSource but i'm not sure how to use it in my case.

Marduk
  • 359
  • 4
  • 13
  • `I read something about async/await and TaskCompletionSource but i'm not sure how to use it in my case.` What did you find when you did some research on what this feature is? What did you find in the tutorials on the subject that you looked at? What problem(s) have you had in your initial testing or experimentation with these features? Clearly you're on the right track. Keep following it. – Servy Apr 09 '15 at 17:29
  • Well I dont event know if my aproach is correct. I would like to read some articles about how this type of case is handled in other projects. – Marduk Apr 09 '15 at 17:33
  • And if you continue learning more about the topic, experiment with it a bit, do some research on how it's used, and how others have used it to solve this problem, then you'll figure that out. – Servy Apr 09 '15 at 17:34

0 Answers0