Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve disconnection of SerialDevice #186

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -613,10 +613,10 @@ private async Task<bool> CheckValidNanoFrameworkSerialDeviceAsync(SerialDeviceIn
{
// dispose on a Task to perform the Dispose()
// this is required to be able to actually close devices that get stuck with pending tasks on the in/output streams
var closeTask = Task.Factory.StartNew(() =>
var task = Task.Factory.StartNew(() =>
{
// This closes the handle to the device
tentativeDevice?.Dispose();
// This closes the handle to the device
tentativeDevice?.Dispose();
tentativeDevice = null;
});
}
Expand Down Expand Up @@ -698,21 +698,21 @@ private async Task<bool> PingDeviceLocalAsync(SerialDevice tentativeDevice)
// write pingHeader to device
outputStreamWriter.WriteBytes(pingHeader);

storeAsyncTask = outputStreamWriter.StoreAsync().AsTask(cts.Token.AddTimeout(new TimeSpan(0, 0, 1)));
storeAsyncTask = outputStreamWriter.StoreAsync().AsTask(cts.Token.AddTimeout(new TimeSpan(0, 0, 2)));

var txBytes = await storeAsyncTask;

//////////////////////////////////////////////////
// write pingPayload to device
outputStreamWriter.WriteBytes(pingPayload);

storeAsyncTask = outputStreamWriter.StoreAsync().AsTask(cts.Token.AddTimeout(new TimeSpan(0, 0, 1)));
storeAsyncTask = outputStreamWriter.StoreAsync().AsTask(cts.Token.AddTimeout(new TimeSpan(0, 0, 2)));

txBytes = await storeAsyncTask;

//////////////////////////////////////////////////
// read answer (32 bytes)
loadAsyncTask = inputStreamReader.LoadAsync(32).AsTask(cts.Token.AddTimeout(new TimeSpan(0, 0, 1)));
loadAsyncTask = inputStreamReader.LoadAsync(32).AsTask(cts.Token.AddTimeout(new TimeSpan(0, 0, 2)));

UInt32 bytesRead = await loadAsyncTask;

Expand Down Expand Up @@ -744,10 +744,12 @@ private async Task<bool> PingDeviceLocalAsync(SerialDevice tentativeDevice)
{
// detach stream
outputStreamWriter?.DetachStream();
outputStreamWriter?.Dispose();
outputStreamWriter = null;

// detach stream
inputStreamReader?.DetachStream();
inputStreamReader?.Dispose();
inputStreamReader = null;
}
}
Expand Down