Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Thread under Android



Permlink Replies: 8 - Last Post: Aug 4, 2015 10:37 AM Last Post By: Remy Lebeau (Te...
Carsten, Andre ...

Posts: 11
Registered: 7/26/04
Thread under Android
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 3:17 AM
Hello,

how to use a thread under Android, is it the same like on a Windows platform?
Any available examples?

regards
Casi
Borja Serrano

Posts: 172
Registered: 1/10/13
Re: Thread under Android
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 6:02 AM   in response to: Carsten, Andre ... in response to: Carsten, Andre ...
Hello,

how to use a thread under Android, is it the same like on a Windows platform?
Any available examples?

I find this post very instructive:

http://blog.marcocantu.com/blog/2014_may_background_delphi_android_threads.html

It seems that it is temporally down, but you can see it through Google cache (http://webcache.googleusercontent.com/search?q=cache:b9-CVw2XyqYJ:blog.marcocantu.com/blog/2014_may_background_delphi_android_threads.html)
Carsten, Andre ...

Posts: 11
Registered: 7/26/04
Re: Thread under Android
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 8, 2014 3:10 AM   in response to: Borja Serrano in response to: Borja Serrano
I am using TThread instead, it works!

Edited by: Carsten, Andre Falticska on Jul 8, 2014 4:57 AM
Ian Calegory

Posts: 4
Registered: 8/9/15
Re: Thread under Android
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 9, 2015 12:12 AM   in response to: Carsten, Andre ... in response to: Carsten, Andre ...
Another option is to use Boost threads. An advantage of using Boost is that it can be used for cross-platform development in code that will run on Windows, Android, and Mac OS X. (iOS, too, for that matter, if the iOS libraries are installed.)

I've compiled the Boost 1.53 C++ libraries for Android (wow what a banging-head-against-the-wall project that was!) so they can be used in Android and FireMonkey applications. Check out my page about this, which describes how to use them, and where the library files can be downloaded from.

[http://www.mandelmania.net/boost.html]

Ian
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
Re: Thread under Android
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 28, 2015 7:53 PM   in response to: Carsten, Andre ... in response to: Carsten, Andre ...
Hi Casi,

> how to use a thread under Android, is it the same like on a Windows platform?
- There is an API "Synchronize()" recommended as part of the TThread remark generated for Windows. However, I had no problem running TThread without using Synchronize() on previous version of C++Builder.

// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize( &UpdateCaption );
//
// where UpdateCaption could look like:
//
// void __fastcall TXThread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }

> Any available examples?
- You may refer to the following links:

http://docwiki.embarcadero.com/CodeExamples/XE8/en/TThreadPriority_(C%2B%2B)

NOTE: Make sure to create a new thread object via:

File->New->Other->C++Builder Projects->C++Builder Files->Thread Object

Here is the trick I used to call main form method ( eg. f_iOnThreadProc() ) from Thread's Execute():

1. Includes the thread header ( eg. "oThread.h" ) in the MainH header file.

#include <IdThreadComponent.hpp>
#include "oThread.h"

2. Under oThread.cpp where "eg. void __fastcall TXThread::Execute()" resided, put the following lines:

#include "oMain.h"
TfMain* fMain;

NOTE: MAKE SURE TO REMOVE THE LINE #include "oThread.h" from oThread.cpp

3. Now, you can call f_iOnThreadProc() under "Execute()" method such as:

void __fastcall TXThread::Execute()
{
if( !fMain ) return;

while( !m_bStop )
{
// NOTE: You may placed other methods or calculations under f_iOnThreadProc().
fMain->f_iOnThreadProc();

Sleep( 1000 );
}
}

Hopes, this help.

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Thread under Android [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 28, 2015 8:32 PM   in response to: GAI CHEW KAI in response to: GAI CHEW KAI
GAI wrote:

- There is an API "Synchronize()" recommended as part of the
TThread remark generated for Windows. However, I had no problem
running TThread without using Synchronize() on previous version of
C++Builder.

Read the comment more closely. Synchronize() does not run the thread. Synchronize()
simpy allows the thread to safely access methods/properties of VCL object
that belong to the main thread.

- You may refer to the following links:
http://docwiki.embarcadero.com/CodeExamples/XE8/en/TThreadPriority_(C%2B%2B)

That is a good example of what NOT to do with TThread. The code inside of
those Execute() methods is NOT safe. Whoever wrote that example needs to
have their hand slapped!

NOTE: Make sure to create a new thread object via:

*File->New->Other->C++Builder
Projects->C++Builder Files->Thread Object*

That is merely a shortcut, NOT a requirement.

*Here is the trick I used to call main form method ( eg.
f_iOnThreadProc() ) from Thread's Execute():*

Since you are not synchronizing f_iOnThreadProc, you have to make sure the
code inside of it is thread-safe.

#include <IdThreadComponent.hpp>

You are confusing TThread with TIdThreadComponent.

Hopes, this help.

Not really, actually.

--
Remy Lebeau (TeamB)
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
Re: Thread under Android [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 3, 2015 7:07 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
> That is a good example of what NOT to do with TThread. The code inside of
> those Execute() methods is NOT safe. Whoever wrote that example needs to
> have their hand slapped!
- I have been using this approach for years starting CB3.0, is ok though :)

> That is merely a shortcut, NOT a requirement.
- I try to implement without the so called "shortcut" but it does not work.

> You are confusing TThread with TIdThreadComponent.
- Well, it is there when new thread object is created.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Thread under Android [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 4, 2015 10:37 AM   in response to: GAI CHEW KAI in response to: GAI CHEW KAI
GAI wrote:

- I have been using this approach for years starting CB3.0, is ok
though :)

Then you have been doing it wrong this whole time, and have been lucky your
app hasn't crashed/deadlocked itself.

I try to implement without the so called "shortcut" but it does not work.

Then you must be doing something incorrect, because I write TThread classes
all the time and rarely ever use the "TThread Object" wizard at all. So
I know implementing TThread classes manually works fine when done correctly.
There is nothing really special to do it. Derive a new class from TThread,
override the virtual Execute() method (and optionally the constructor, destructor,
DoTerminate(), etc), and then use your class where needed. Same functionality
the "Thread Object" wizard provides.

Well, it is there when new thread object is created.

The "Thread Object" wizard does not create a TIdThreadComponent, or include
IdThreadComponent.hpp into your code. It creates a TThread-derived class,
and includes Classes.hpp instead. TIdThreadComponent is an Indy component,
and like any other third-party component the ONLY way it could appear in
your code is if you place it there yourself. At some point, you must have
dropped TIdThreadComponent onto your Form at design-time.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Thread under Android
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 28, 2015 8:33 PM   in response to: Carsten, Andre ... in response to: Carsten, Andre ...
Andre wrote:

how to use a thread under Android, is it the same like
on a Windows platform?

Yes. The RTL's TThread class works exactly the same way on all platforms.
Define a class that derives from TThread and override its virtual Execute()
method. Then create an instance of your class at runtime when needed.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02