Author Topic: Is wclSyncClient thread-safe?  (Read 805 times)

Offline luigidallavalle

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Is wclSyncClient thread-safe?
« on: June 07, 2016, 12:23:19 PM »
Hi All,
    I have an windows form application that communicates (reads\writes) with a device through SPP.
The typical communication cycle is:
1 the application sends a command
2 the application wait for a response, which is always and quickly sent back by the device.

I have a wclSyncClient which is created in the UI thread at the application start (the channel is opened at the same time), then it's used in both the UI thread by some event handler that responds to user actions, and in other background threads. Every access to the client is synchronized through locks.
What happens is that, sometimes, in the background threads, the Read method cannot receive anything, I also have a retry policy, Read is called several times, always with 0 byte read.
After that, when the users triggers a command by using the UI, all the data I was expecting are now received by a the first Read attempt.

I noticed that the odd behaviour occours mostly when the background operation is performed immediatly next to a UI triggered one.

Because of that I suspect that the wclSyncClient is not thread-safe, what are your thoughts?

Thanks in advance,
Luigi

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3053
  • Karma: 1000
    • Soft Service Company
Re: Is wclSyncClient thread-safe?
« Reply #1 on: June 07, 2016, 01:00:18 PM »
Hello,

The wclSyncClient must be used in the same thread it is created/connected. Also if you use it in non-guid thread the thread must have a message loop.

For your case I recomend to use asynchronous wclClient instead of wclSyncClient.

Offline luigidallavalle

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Re: Is wclSyncClient thread-safe?
« Reply #2 on: June 07, 2016, 02:07:57 PM »
Thanks a lot for the quick response!

Offline luigidallavalle

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Re: Is wclSyncClient thread-safe?
« Reply #3 on: June 10, 2016, 12:24:49 PM »
I have the same problem using wclClient too, but I'm not sure if I'm doing everything in the right way:
in the main thread, the wclClient is created and onData event handler is assigned.

When waiting for data from a background thread what appens is that the onData event is never triggered, it is fired when the control passes to the main thread.
is it the correct behaviour?

If it is, what I need to do is to subscribe \ unsubscribe che event handler every time I need to wait for data to come, in thaw way I'll be sure that che event subscription is made in the same thread which sends the data, is it correct?

thanks Mike

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3053
  • Karma: 1000
    • Soft Service Company
Re: Is wclSyncClient thread-safe?
« Reply #4 on: June 10, 2016, 12:39:44 PM »
with wclClient you do not need any background thread as it is asynchronouse.

If you want to use it in threads then your thread must have a message loop and the wclClient object must be created and opened in thread where it is used.

 

Sitemap 1 2 3 4 5 6 7 8 9