TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: All
from: Julien Pierre
date: 1995-01-08 02:16:16
subject: IThread: Any example code??

Bonjour, All!

I'm forwarding this message from usenet because I think it may be useful to
some of you.

======================================================
* Original dans la zone COMP.OS.OS2.PROGRAMMER.MISC
* Original de Julien Pierre 999:320/215
* Original pour Peter Murray 
* Original a propos de "IThread: Any example code??"
======================================================


Bonjour, Peter!

Suite au message de Peter Murray a All:

 PM> From: pcm{at}scammell.ecos.tne.oz.au (Peter Murray)


 PM> Has anyone got any sample code demonstrating the use
 PM> of IThread in IBM CSet++?  The sample in the supplied
 PM> documentation is minimal to say the least!

It's very simple.
Basically, you need to create a class inherited from IThreadFn that does
your job, then create an IThread to start that function.

Suppose the main window code in your application is in your
"AMMWindow" class.
You want to play music in the background with your AMMWindow::play() member
function and need an additionnal thread.
(that's indeed a real-world example from my beta mod player :-)))

Here is sample code :

/******************************************************************************/
/* Class   : PlayThreadFn
/*                                                                            */
/* Purpose : this class can be used to execute the member function            */
/*           AMMWindow::play()
/*           on a separate thread.                                            */
/*           It is a subclass of IThreadFn                                    */
/*                                                                            */
/******************************************************************************/
class PlayThreadFn : public IThreadFn                                           
{
  public:
    PlayThreadFn(AMMWindow &obj) : object(obj) {;}                              
    void run() { object.play(); }                                               
  private:                                                                      
    AMMWindow &object;                                                          
};                                                                               

Here is the code I use to dispatch the thread :

 *
 *
 .
 case MI_PLAY:                                                             
      if (playing) {                                                       
              return(true);                                                
              break;                                                       
      } else {                                                             
      playing = True;                                                      
      display = true;                                                      
      menuBar->disableItem(MI_PLAY);                                       
// Note for P. Murray : only the two following lines are thread-related
      PlayThreadFn *playFn = new PlayThreadFn( *this);                     
                                      // dispatch thread to run function   
                                      // thread will have a PM environment 
      IThread thread( playFn );                                            
      return(true);                                                        
      break;                                                               
      };                                                                   
 .
 *

Hope this helps !

About the ICLUI :
It's true that the UICL documentation is not very helpful. It's only a
reference and there is no real guide - except the 100 page hardcopy manual
is a good introduction but is not sufficient in complex cases (for one, it
doesn't describe multi-threading).

Two days ago, I went to the "OS/2 Boutique" in Paris, a computer
shop that's entirely dedicated to OS/2 products, and I found a book called
"OS/2 Class Library", edited by VNR.
I HIGHLY RECOMMEND THIS BOOK.
It's written by the authors of ICLUI. It's about 900 pages and has a
diskette filled with sample code (unpacks to 2.7MB on the HD), which is
explained in great detail throughout the book.
I had to pay 340F for it (or about US$60); but I'm pretty sure it will cost
you less than half that in the US. Technical books always cost a fortune
here, especially foreign books. I'm sure you won't believe me if I tell you
that $60 for this book was a real bargain : the few shops that carry
technical programming books that do not exist in french usually sell them
for twice that, or more !!! Even fewer of them carry OS/2 books. Much less
OS/2 programming books. 
It was a real shock for me to find that book. I immediately bought it.
But I digress. Even at that price, I certainly haven't regretted it. To me,
that book really is INVALUABLE. It's a lifesaver.
Considering that it's going to allow me to finish my product - which would
otherwise have taken 2 years with the ICLUI reference, or maybe dropped - I
think that this book is going to pay off MANY times its price.
I still can't believe how good it is.
I had never coded with an object-oriented model in the past, nor had I any
experience with GUI code, so I really can't say I was comfortable with
ICLUI, especially in the absence of a real manual.

I found myself reading one of the code samples in the book and wondering
about a strange C++ syntax :
dontBuyList
.setTitle("Unneeded items")
.showTitle()
.showTitleSeparator)
.showIconView()
.arrangeIconView()
.setDeleteObjectsOnClose();

I was going to lookup in my C++ language book when I saw a special outlined
notice just below this code : one half page titled "Chaining
Functions" that began with :
"Perhaps you are wondering about the syntax used to set up the two
containers..."
and explained this exact syntax very clearly in one half of a page.
The authors just seem to read minds !

Overall, this book is really wonderful.
It's not just a guide, it's an excellent reference too. In 5 minutes, I
found a *full* solution with code to the problems I was having with
sliders. I didn't know how to be notified automatically for action when the
value of the slider had changed, I had to use an "Apply" button,
which was *very* annoying for a volume control. Even though it *had* to be
possible, I had never been able to track the right class to do this in the
ICLUI reference that comes with C Set ++. But with this guide, I just went
to chapter 12, "Slider", and immediately found the solution in
the "Slider Arm Operations" paragraph, which was to use an
IEditHandler object.

IMHO this book is really a must, and should be included with ICLUI. Now
that I have it, I expect to be about 100 times more productive. A 10000%
increase in productivity may sound like a lot, but not if you are learning
C, C++, PM, MMPM/2 and OS/2 programming all at once (can you spell MAD :-)?
I knew you could)

Julien
[Team OS/2 FR]

-!- I receive 30+ personal mails daily, so do not expect a quick answer.
 ! Origin: ChaOS/2 - the Team OS/2 BBS France - +33-1-30472772 (999:320/215)

==================== Fin de la retransmission ====================


A bientot, Julien!

 [Team OS/2 FR]

--- I receive 30+ personal mails daily, so do not expect a quick answer.
* Origin: ChaOS/2 - the Team OS/2 BBS France - +33-1-30472772 (2:320/0)
SEEN-BY: 12/2442 620/243 624/50 632/348 640/820 690/660 711/409 410 413 430
SEEN-BY: 711/807 808 809 934 942 949 712/353 515 713/888 800/1 7877/2809
@PATH: 320/0 321/1 283/512 280/801 24/24 396/1 3615/50 229/2 12/2442 711/409
@PATH: 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.