earamsey
Posts: 3
Joined: Wed Sep 17, 2008 9:41 pm
Location: USA

How to abort call ConnectService()

Tue Jun 02, 2009 8:52 pm

I am using .NET Compact Framework 3.5 (Windows Mobile 6).

Scenario:
1. User requests a connection to bluetooth device from GUI Form and a background thread created to process it.
2. The bluetooth device is not responding, for some reason.
3. User presses abort button on GUI Form to abort connection.

Problem: I can kill background thread, via Thread.Abort(), but would Franson actually abort it's attempt?

Code: Select all

 1| public void Connect(Address address, string deviceName, int serviceChannelNumber, string serviceName) {
 2|    try {
 3| 	  bluetoothDevice = network.ConnectDevice(address, deviceName);
 4| 	  commandService = bluetoothDevice.ConnectService(serviceChannelNumber, serviceName);
 5| 	  bool b = commandService.Stream.CanRead;
 6|    }
 7|    catch (Exception e) {
 8| 	  // handle BlueToolsExccception or ThreadAbortException
 9| 	  return false;
10|    }
11| return true;
12| }
Question:How can lines #3 and #4 be cleanly aborted? I am told that thread.abort just wreaks havoc in .NET and just leads to more problems. Can you please help?

Thanks,

Erica A Ramsey
Mobile Application Developer
Resume ~ http://home.comcast.net/~erica.ramsey/CV
Some Project Screenshots ~ http://home.comcast.net/~erica.ramsey/CV/screens.html

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: How to abort call ConnectService()

Wed Jun 03, 2009 9:13 am

I'm not sure if anything really happens until you request the Stream.
In any case, you can't abort anything before that.

Regards,
Jonas

earamsey
Posts: 3
Joined: Wed Sep 17, 2008 9:41 pm
Location: USA

RE: How to abort call ConnectService()

Fri Jun 05, 2009 12:27 am

Well, what are my alternatives? I'm quite sure calling System.Threading.Thread.Abort() on Franson's thread of execution will kill it; I think .NET Manager forces it to die. However, this could leave both Franson & .NET Compact Framework in a bad state. In the case of Franson, a recovery can be attempted by closing any open Stream belonging to a RemoteService and calling Manager.GetManager().Dispose(); however, it appears that calling Manager.Dispose() renders Franson unusable until application restarted; or is there a way to do this?

In summary, my solution;Show dialog notifying Franson is busy;Option to WaitOption to Abort
On AbortTerminate Franson's thread of execution via System.Threading.Thread.Abort()Close any open Stream's on RemoteServicesIdeally call Manager.GetManager().Dispose() but unfortunately this renders Franson unusable until application is restarted.Display message to suggest bluetooth device is resetIs this the best that can be done?


Thanks,

Erica A Ramsey
Mobile Application Developer
Resume ~ http://home.comcast.net/~erica.ramsey/CV
Some Project Screenshots ~ http://home.comcast.net/~erica.ramsey/CV/screens.html

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: How to abort call ConnectService()

Fri Jun 05, 2009 12:35 am

As I said, I don't think like 3 & 4 needs to be aborted. You are just setting up the parameters of what device you wish to connect too, it isn't until you request the Stream on line 5 that you are actually connecting.

Once you have the Stream connection you can simply close it to disconnect/abort.

Regards,
Jonas

earamsey
Posts: 3
Joined: Wed Sep 17, 2008 9:41 pm
Location: USA

RE: How to abort call ConnectService()

Sun Jun 07, 2009 11:03 pm

Yes, line #5 (commandService.Stream.CanRead) causes the problem. SOrry, that is what I was meaning to day. Is above fix acceptable in this situation?

Thanks,

Erica A Ramsey
Mobile Application Developer
Resume ~ http://home.comcast.net/~erica.ramsey/CV
Some Project Screenshots ~ http://home.comcast.net/~erica.ramsey/CV/screens.html

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: How to abort call ConnectService()

Mon Jun 08, 2009 9:18 am

Closing the Stream is enough to abort.

Regards,
Jonas

Return to “BlueTools”