Author Topic: OPPClient and multiple threads  (Read 9931 times)

Offline alexey_gusev

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
OPPClient and multiple threads
« on: June 01, 2009, 07:21:30 AM »
Hello,

I'm using several wclOPPClient objects to send files to the phone. The whole procedure is asynchronous. At one place I BeginInvoke() some starting method, in this method I create an object that encapsulate an array of wclOPPClient (say, WCLSender) and call WCLSender .SendFiles():

            sender = new WCLSender(arrBtAddr, arrFiles, senderWaitTime);
            eventFileSending = new EventFileSending(sender.SendFiles);
            eventFileSending.BeginInvoke(new AsyncCallback(OnFileSendingFinish), eventFileSending);

In completion method I do some post-processing of the files sent (or rejected, or timed out).

That's where I have a question. I've defined an array of ManualResetEvent in my WCLSender class, so they may be Set() at various stages. In sender.SendFiles(), I call Connect() on all wclOPPClient objects. Then I want to just wait in SendFiles() until all handlers signal either error or completion (by setting manual event). If I wait for events to be signalled on the same thread where I call Connect(), my OnConnect callbacks are never called. If I call wclOPPClient.Connect() on the different thread and wait for the same events in SendFiles() - I do hit the callbacks. Threads must continue looping, otherwise callbacks are not called.

Am I completely wrong here with such design or missing something obvious?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: OPPClient and multiple threads
« Reply #1 on: June 01, 2009, 10:42:13 AM »
Hello,

You are doing all correct except one thing. Your thread must have message loop to be "alive" to get events. The MFC edition includes the sample for that. I am sure, the same can be implemented in .NET too.
The other idea is do not use threads (like ProximitySender implemented).

Offline alexey_gusev

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Re: OPPClient and multiple threads
« Reply #2 on: June 01, 2009, 10:50:02 AM »
OK, I see, so if I wait on the event, I then block the current thread (and as a result no incoming messages).
Is it hard to implement it in such way that one can initiate the sending (calling Connect() ) and then wait on event right after the call, ie to be based on event handlers rather than messages?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: OPPClient and multiple threads
« Reply #3 on: June 01, 2009, 10:55:18 AM »
Hi,

Yes, you are right, when you wait on event you block the messaging.
Unfortunatly it is impossible because internal stacks implementation. But I am still wondered why you use threads or asynchronious library :) It is easy to implement all in one thread and handle all the things without problems.

Offline alexey_gusev

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Re: OPPClient and multiple threads
« Reply #4 on: June 01, 2009, 11:03:46 AM »
well, I'd love to avoid using threads, believe me :)
unfortunately I've inherited the C# application which is complex and multithreaded, with MANY BeginInvoke() calls, so I needed some firm grounds. At some point discovery and sending were implemented as separate console applications which reported back to the main one via pipes  :D . I've moved their code into the main app recently.

So the main idea is to kick off the discovery, then, if user wanted to send files, invoke sending part and finally post-process the files to ensure e.g. that they won't be resent again. I admit it may be redesigned to have all processed in the main thread without any threads, but I'd like to get to the dead end first :)

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: OPPClient and multiple threads
« Reply #5 on: June 01, 2009, 11:18:14 AM »
Ok, I see

I'll try to create a C# sample with using WCL in threads today or tomorrow and post it here then. Will it be ok?

Offline alexey_gusev

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Re: OPPClient and multiple threads
« Reply #6 on: June 01, 2009, 11:20:13 AM »
absolutely! thanks a lot for that

Offline alexey_gusev

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Re: OPPClient and multiple threads
« Reply #7 on: June 01, 2009, 12:59:35 PM »
some unrelated question: how safe is to call Terminate() for wclOPPClient when it doesn't send anything? is it enough to call just Disconnect() to be sure it cleans all up and closes gracefully?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: OPPClient and multiple threads
« Reply #8 on: June 01, 2009, 01:57:45 PM »
Yes, if there is nothing sending - call Disconnect(). Teminate sends some data to the device and it must be called only when sending in progress. Also you can terminate sending with simple Disconnect call. Terminate just terminates current sending but does not close the connection.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: OPPClient and multiple threads
« Reply #9 on: June 04, 2009, 12:16:54 PM »
Hi,

I promised the sample. It is more or less finished and will be included in next release which is planned for ths weekend. Is it OK?

Offline alexey_gusev

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Re: OPPClient and multiple threads
« Reply #10 on: June 04, 2009, 12:21:46 PM »
yeah, that's great, thanks

Offline alexey_gusev

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Re: OPPClient and multiple threads
« Reply #11 on: June 08, 2009, 05:40:38 PM »
hello Mike,

is multithreaded sample included into new release? if yes, please let me know where to look

BR,
Alex

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: OPPClient and multiple threads
« Reply #12 on: June 08, 2009, 05:54:11 PM »
Hi,

yes, it is included in the package. It should be at the same folder where other samples are.

Offline alexey_gusev

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Re: OPPClient and multiple threads
« Reply #13 on: June 08, 2009, 05:57:34 PM »
could you tell me the folder name please?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: OPPClient and multiple threads
« Reply #14 on: June 08, 2009, 06:11:33 PM »
OPPThreads

 

Sitemap 1 2 3 4 5 6 7 8 9