-1

I am working on signalR. I have a server (ASP .NET) and a client (C# WinForms). In my client app I have a async method:

public async void conAsync()
{
    HubConn = new HubConnection(serverURL);
    HubPrx = HubConn.CreateHubProxy("myHUB");

    try
    {
        await HubConn.Start();
        richTextBox1.AppendText("connected");
        button1.BackColor = Color.Green;

        groupBox1.Enabled = true;
        groupBox2.Enabled = true;        
    }
    catch(Exception err)
    {
        // deactive comps
        groupBox1.Enabled = false;
        groupBox2.Enabled = false;        

        richTextBox1.AppendText(err.toString());
    }
}

The Start Button executes the above method. I want provide a Stop Button which stops the connection to the server. I read about CancellationToken but I got confused about how to use it in my case. Actually my button is a CheckBox which acts like a ToggleButton.

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked)
    {
        checkBox1.ForeColor = Color.Green;
        conAsync();
    }
    else
    {
        //stop conAsync() here
    }
}
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Rouzbeh Zarandi
  • 1,047
  • 2
  • 16
  • 34
  • 1
    Dup - https://stackoverflow.com/questions/10134310/how-to-cancel-a-task-in-await – Yacov Jun 20 '17 at 07:46
  • See above link and also "HubConn.Stop();" ?? – Tez Wingfield Jun 20 '17 at 07:50
  • @TezWingfield if i use HubConn.stop().... am i supposed to get execution of catch() in my function ? after HubConn.stop() executed – Rouzbeh Zarandi Jun 20 '17 at 07:52
  • @Yacov that sample actually confused me... :| in my case how to pass the token .... conAsync(token) ? – Rouzbeh Zarandi Jun 20 '17 at 07:55
  • 1
    Firstly I think we have to address "code smells"... What your doing here is adding "flow" into "exceptions" - I'm not familiar with "WinForms" but the fundamentals still apply. Hide/Show controls based on whether you have an active connection. Exceptions handling not for the display of UI. Once at that point, it will become a lot easier to handle. – Tez Wingfield Jun 20 '17 at 08:05
  • 1
    @TezWingfield that's not flow. That code *does* **enable** the controls once there is an active connection. It also logs the error and disables the controls just to be safe. You can argue that the UI's state should be a separate class, but that adds complexity and is irrelevant to the question. WinForms data binding is finicky anyway – Panagiotis Kanavos Jun 20 '17 at 08:09
  • 1
    @PanagiotisKanavos I would argue that, here we are using a "catch" to disable the controls... Not ideally what you would do. Use the "catch" to log but "IsActive" to display the controls but again I'm not familiar with "WinForm" best practises. Semantics ;) – Tez Wingfield Jun 20 '17 at 08:13
  • @TezWingfield not semantics. What you say has nothing to do with the issue, nor will it make it any easier to fix it. Yes, a separate state object, or a full MVVM model is better. This has nothing to do with the question though. What would be relevant, would be to ask the OP to use proper logging instead of just writing to an RTF box – Panagiotis Kanavos Jun 20 '17 at 08:39
  • @RouzbehZarandi how is `conAsync` called? `async void` is only meant for exception handlers. It can't be awaited or cancelled, it's essentially fire-and-forget. – Panagiotis Kanavos Jun 20 '17 at 08:41
  • @PanagiotisKanavos private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked) { conAsync(); }else{........}} and on else i want stop the connection..........on previous practice i only called conAsync() on form_load...and it works properly – Rouzbeh Zarandi Jun 20 '17 at 08:50
  • i think the best solution is cancellation token ... but since this is the first time i heard about that ..still i have confusion to use it in my case ..... but in other hand another solution may be HubConn.stop() but my doubt is that i use HubConn.stop() outside of conAsync() so what will happen to my conAsync() execution ......? – Rouzbeh Zarandi Jun 20 '17 at 08:57

1 Answers1

2

HubConn.close() solved the problem and it stops the conAsync() ..... i think the cancellation token is not the solution here (i was wrong) but it may be alternative one for another case.

Rouzbeh Zarandi
  • 1,047
  • 2
  • 16
  • 34