Author Topic: Creating WCL objects in threads (C++ Builder XE5)  (Read 2212 times)

Offline madjoe

  • Newbie
  • *
  • Posts: 1
  • Karma: 0
Creating WCL objects in threads (C++ Builder XE5)
« on: June 29, 2014, 11:06:55 PM »
Hello,

I am using Embarcadero C++ Builder XE5 and the latest WCL for VCL.

When i create TwclAPI, TwclBluetoothDiscovery and TwclClient over the IDE in a Form, everything works fine.
I can connect to other devices and event handlers like OnConnect or OnData are called correctly.

Since it is common, that we are using threads for automated things, i want to use WCL in threads.
Therefore i created objects like this:


class BluetoothThread : public TThread
{
private:
   TwclBluetoothRadio* __fastcall GetSelectedRadio();

protected:
   void __fastcall Execute();
   void __fastcall DoTerminate();

public:
   TwclAPI *wclAPI1;
   TwclBluetoothDiscovery *wclBluetoothDiscovery1;
   TwclClient *wclClient1;

   void __fastcall wclClient1Connect(TObject *Sender, int Error);
   void __fastcall wclClient1Disconnect(TObject *Sender);
   __fastcall BluetoothThread();

   __property Terminated;
};

TwclBluetoothRadio* __fastcall BluetoothThread::GetSelectedRadio()
{
   TwclBluetoothRadio* Result = NULL;
   TwclBluetoothRadios* Radios = new TwclBluetoothRadios();
   if (!wclShowError(wclBluetoothDiscovery1->EnumRadios(Radios)) && Radios->Count > 0)
   {
      Result = new TwclBluetoothRadio();
      Result->Assign(Radios->Items[0]);
   }

   return Result;
}

void __fastcall BluetoothThread::Execute()
{
   wclAPI1 = new TwclAPI(NULL);
   wclBluetoothDiscovery1 = new TwclBluetoothDiscovery(NULL);
   wclClient1 = new TwclClient(NULL);
   wclClient1->OnConnect = wclClient1Connect;
   wclClient1->OnDisconnect = wclClient1Disconnect;
   wclClient1->BluetoothParams->Authentication = false;
   wclClient1->BluetoothParams->Channel = 0;
   wclClient1->BluetoothParams->Encryption = false;
   wclClient1->BluetoothParams->Address = "(D4:94:A1:54:32:8F)";
   wclClient1->BluetoothParams->Radio = Radio;
   wclClient1->BluetoothParams->Service = SerialPortServiceClass_UUID;
   wclClient1->ConnectTimeout = 20000;
   wclClient1->Transport = trBluetooth;
   wclAPI1->Load();

   TwclBluetoothRadio* Radio = GetSelectedRadio();
   if (Radio)
   {
      wclShowError(wclClient1->Connect());
      Radio->Free();
   }

......

My problem is, that when i try to connect the wclClient1, it stays in FState = scConnecting and never reaches the event handler OnConnect.

What could be wrong here?
Maybe i need to change the method creating WCL objects?

Thanks for any help
madjoe

Online Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: Creating WCL objects in threads (C++ Builder XE5)
« Reply #1 on: June 30, 2014, 08:28:47 AM »
Hello,

As WCL is asynchronous I do not recomend to use it in threads (as it does not block main thread). But if you exactly need WCL in thread then:

1. TwclAPI must be always in main thread
2. Other WCL objects must be created in thread which uses it.
3. Thread with WCL object must have Windows Message Loop

 

Sitemap 1 2 3 4 5 6 7 8 9