c#


How to stop a call resulting in IAsyncOperation?


In my application I notify multiple devices. If a device is not reachable and is not responding within a few seconds, i want to cancel the call.
My code is:
await characteristic0.WriteClientCharacteristicConfigurationDescriptorAsync
(GattClientCharacteristicConfigurationDescriptorValue.Notify);
Now after some research i found out that usually one can pass a CancelationToken (_ct) and do this:
First create an operation containing the call:
IAsyncOperation<GattCommunicationStatus> operation = characteristic0.WriteClientCharacteristicConfigurationDescriptorAsync
(GattClientCharacteristicConfigurationDescriptorValue.Notify);
Then creating a task with a CancellationToken:
Task<GattCommunicationStatus> task = operation.AsTask(_ct);
And then await it:
GattCommunicationStatus status = await task;
Now, the thing is even though the IsCancellationRequested-Property of the CancellationToken is set to true. The call wont stop.
And, the Device is getting Notified after the first line already! Isn't that supposed to happen after the call to await??
Do i make a mistake with the Token or is this a bigger thing?
EDIT
After the conversation with #Andrii Litvinov i put some more code to my initial description of the problem. Here is the whole method:
public async Task<GattCommunicationStatus> NotifyDevice(DeviceInformationDisplay deviceInfo, CancellationToken _ct)
{
try
{
BluetoothLEDevice device = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id);
service = device.GetGattService(new Guid(Service_UUID));
characteristic0 = service.GetCharacteristics(new Guid(Characteristic_0_UUID)).First();
characteristic0.ValueChanged += characteristic0ValueChanged;
GattCommunicationStatus status = await characteristic0.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
_ct.Register(() =>
{
// trying to cancel the operation somehow
});
IAsyncOperation<GattCommunicationStatus> operation = characteristic0.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
Task<GattCommunicationStatus> task = operation.AsTask(_ct);
GattCommunicationStatus status_1 = await task;
if (!elapsedTimeWatcher.IsRunning)
{
elapsedTimeWatcher.Start();
}
else
{
elapsedTimeWatcher.Restart();
}
return status;
}
catch(OperationCanceledException e)
{
return GattCommunicationStatus.Unreachable;
}
catch (Exception e)
{
return GattCommunicationStatus.Unreachable;
}
}
Based on the docs you are doing it right:
try
{
...
IAsyncOperation<GattCommunicationStatus> operation =
characteristic0.WriteClientCharacteristicConfigurationDescriptorAsync(
GattClientCharacteristicConfigurationDescriptorValue.Notify);
return await operation.AsTask(_ct);
...
}
...
This code should register to token and call operation.Cancel() behind the scene.
Can you set a token to lower value to see if operation is actually cancelled? How much time does it normally take to execute the method?
Try:
CancellationTokenSource cts = new CancellationTokenSource(10);
await NotifyDevice(BLEDevice, cts.Token);
Or even lower values.

Related Links

How to add a result tolerance to a NUnit TestCase
Id POST as hidden field but null in controller
TypeBuilder set type size
how to get all dna encoding for peptide in c#
I still can't clear the rectangle i drawn over a control inside OnPaint event why i can'y clear it?
MVC Bundle Virtual Path Not Found
String.Compare doesn't work on greater case
Adding a parameter on the ActionArguments
Inner Join in Wpf Mvvm Architecture
SQL Server 2012 SqlCLR reads configurations from machine.config instead of sqlservr.exe.config
PhoneBootStrapper namespace in windows phone
SendKeys in c# - not able to send multiple keys
How to download PDF file using link button?
Connecting to an mdb file using OdbcConnection in VS2013
Entity Framework - updating record
Custom Class Converting in c#

Categories

HOME
gcc
kendo-ui-angular2
sip
packet
microcontroller
latex
spotfire
mailchimp
thermal-printer
glympse
procmon
pyodbc
jetbrains
xmlhttprequest
background-image
openscad
novacode-docx
beacon
ejb-3.1
javaagents
checkmarx
recreate
multilanguage
voyager
aweber
motherboard
console.readline
latency
mybatis-generator
observer-pattern
idea-plugin
julia-jump
icu
google-chrome-console
collation
irr
datagridviewcombobox
vpython
scrapinghub
dcom
serialversionuid
android-vectordrawable
webmatrix
programming-pearls
chain
android-tabs
mavlink
rowname
qtruby
nimbus
bytearray
database-administration
mod
trendline
database-project
tess4j
consul-template
azure-mobile-app
borland-c++
sparkcore
firewire
genome
minko
accpac
bungeecord
diffmerge
dotcmis
debuggervisualizer
power-management
mixradio
starling-framework
grunt-contrib-imagemin
mobile-robots
caroufredsel
angularjs-select2
typography
wpa
dynamic-expresso
proj4
nsautoreleasepool
vline
feincms
xcode4.6.3
nsscrollview
mysql-connector
advanceddatagrid
visual-leak-detector
code-organization
httppostedfilebase
execjs
service-layer
cpack
system-analysis
web-widget
moores-law

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile