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
}
}
HubConn.close() 解决了这个问题,它停止了 conAsync() ..... 我认为取消令牌不是这里的解决方案(我错了)但它可能是另一种情况的替代方案。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.