Alaska Software Inc. - Multiple Threads
Username: Password:
AuthorTopic: Multiple Threads
Don SchmitzMultiple Threads
on Fri, 22 Sep 2006 09:48:10 -0500
I'm looking for a way to control users of a program that if they have not
done any work in the application for say 15 minutes in the app that it would
shut the app down.  Looks like starting another Thread is the solution, does
anyone have any sample of this kind of soution they would be willing to
share?

Thanks



*********************************************
Don Schmitz
University Hospital Surgery Dept.
600 Highland Ave. F8-164
Madison, WI  53792
(608) 263-9307
Email: Dons@surgery.wisc.edu
Tim SnyderRe: Multiple Threads
on Tue, 26 Sep 2006 08:58:06 -0400
Hi Don,

Perhaps you could explain for us why the earlier solutions didn't work for 
you. There was a lot of good information in those posts. Are you very 
familiar with working with threads or just starting out?

Here are two basic thread concepts:
- Only one event loop per thread.
- Threads stop themselves. They can't be stopped directly from outside; an 
outsider has to send them a message to stop.

I think of a thread as a stand-alone program that is responsible for opening 
and closing data and managing I/O, but it is different only in the fact that 
it may be one of several such programs.

In your situation, if nothing happens in one thread for 15 minutes, you want 
it to shut down the whole program. If it were a stand-alone program it would 
do so. But in your case, it is just one of several programs running. The 
other programs may be active and not want to shut down. It's like several 
children in one car; some want to stop, others want to go on. Dad or Mom 
decides when everyone will stop.

In your app., each thread needs to tell Dad it is ready to stop. When Dad 
gets "ready-to-stop" messages from all the children, Dad tells each one it's 
now OK to stop. They do so and report back after they have stopped. When Dad 
sees all the children have stopped, he pulls over and stops the car.

In your main thread (Dad) establish a "ready-to-stop" status board that hold 
the status for each thread (Child). When there is activity in a thread, it 
sets it's status as "not-ready." If 15 minutes go by without any activity, 
the thread changes its status to "ready." You need a loop in the main 
program that continuously monitors the "ready-to-stop" status board. When 
all threads are in a "ready-to-stop" mode, the main program sends terminate 
messages to all the threads and waits until all threads report back they are 
terminated. Then the main program cleans up itself and quits.

You may not have thought about this yet, but you might want to do so. You 
may want to stop your app via an external method. Pressing alt-C may not 
effect an orderly shutdown of all threads. You may leave connections to 
databases open. For an external trigger I use the presence of a file, 
EXITNOW.TXT. When the file is present the main monitoring loop tells each 
thread to stop. When all threads have stopped, the main program shuts down. 
You could add this feature to your loop that monitors the "ready-to-stop" 
status board.

How about putting together a sample program that implements these concepts 
and show us the code. Then you can ask us specific questions about things 
that give you difficulty.

Tim
Don SchmitzRe: Multiple Threads
on Tue, 26 Sep 2006 13:08:36 -0500
Thanks for the explanation it was very helpful.






*********************************************
Don Schmitz
University Hospital Surgery Dept.
600 Highland Ave. F8-164
Madison, WI  53792
(608) 263-9307
Email: Dons@surgery.wisc.edu
Don SchmitzRe: Multiple Threads
on Thu, 28 Sep 2006 10:01:43 -0500
Again thanks for the info it was helpful.

Yes I'm new to Xbase and to Threads.....
I have included a test program which is my first attempt which doesn't seem
to be honoring the key movement that you would hit in the read statements. 
I'm sure I'm doing something wrong, if you can help it would be appreciated.
 I did take a look at the listserver thread from the other comment but the
end result was over my head.


*********************************************
Don Schmitz
University Hospital Surgery Dept.
600 Highland Ave. F8-164
Madison, WI  53792
(608) 263-9307
Email: Dons@surgery.wisc.edu



>>> Tim Snyder<timothy.snyder@nreis.com> 9/26/2006 7:58 AM >>>
>Hi Don,
>
>Perhaps you could explain for us why the earlier solutions didn't work for
>
>you. There was a lot of good information in those posts. Are you very 
>familiar with working with threads or just starting out?
>
>Here are two basic thread concepts:
>- Only one event loop per thread.
>- Threads stop themselves. They can't be stopped directly from outside; an
>
>outsider has to send them a message to stop.
>
>I think of a thread as a stand-alone program that is responsible for
>opening 
>and closing data and managing I/O, but it is different only in the fact
>that 
>it may be one of several such programs.
>
>In your situation, if nothing happens in one thread for 15 minutes, you
>want 
>it to shut down the whole program. If it were a stand-alone program it
>would 
>do so. But in your case, it is just one of several programs running. The 
>other programs may be active and not want to shut down. It's like several 
>children in one car; some want to stop, others want to go on. Dad or Mom 
>decides when everyone will stop.
>
>In your app., each thread needs to tell Dad it is ready to stop. When Dad 
>gets "ready-to-stop" messages from all the children, Dad tells each one
>it's 
>now OK to stop. They do so and report back after they have stopped. When
>Dad 
>sees all the children have stopped, he pulls over and stops the car.
>
>In your main thread (Dad) establish a "ready-to-stop" status board that
>hold 
>the status for each thread (Child). When there is activity in a thread, it
>
>sets it's status as "not-ready." If 15 minutes go by without any activity,
>
>the thread changes its status to "ready." You need a loop in the main 
>program that continuously monitors the "ready-to-stop" status board. When 
>all threads are in a "ready-to-stop" mode, the main program sends
terminate
>
>messages to all the threads and waits until all threads report back they
>are 
>terminated. Then the main program cleans up itself and quits.
>
>You may not have thought about this yet, but you might want to do so. You 
>may want to stop your app via an external method. Pressing alt-C may not 
>effect an orderly shutdown of all threads. You may leave connections to 
>databases open. For an external trigger I use the presence of a file, 
>EXITNOW.TXT. When the file is present the main monitoring loop tells each 
>thread to stop. When all threads have stopped, the main program shuts
down.
>
>You could add this feature to your loop that monitors the "ready-to-stop" 
>status board.
>
>How about putting together a sample program that implements these concepts
>
>and show us the code. Then you can ask us specific questions about things 
>that give you difficulty.
>
>Tim



TEST.PRG
Tim SnyderRe: Multiple Threads
on Tue, 03 Oct 2006 08:22:14 -0400
don,

I can't open .prg files. Try zipping it first.
Thanks,
Tim

"Don Schmitz" <DONS@surgery.wisc.edu> wrote in message 
news:616f9a17$1a672821$b1c63@news.alaska-software.com...
> Again thanks for the info it was helpful.
>
> Yes I'm new to Xbase and to Threads.....
> I have included a test program which is my first attempt which doesn't 
> seem
> to be honoring the key movement that you would hit in the read statements.
> I'm sure I'm doing something wrong, if you can help it would be 
> appreciated.
> I did take a look at the listserver thread from the other comment but the
> end result was over my head.
>
>
> *********************************************
> Don Schmitz
> University Hospital Surgery Dept.
> 600 Highland Ave. F8-164
> Madison, WI  53792
> (608) 263-9307
> Email: Dons@surgery.wisc.edu
>
>
>
>>>> Tim Snyder<timothy.snyder@nreis.com> 9/26/2006 7:58 AM >>>
>>Hi Don,
>>
>>Perhaps you could explain for us why the earlier solutions didn't work for
>>
>>you. There was a lot of good information in those posts. Are you very
>>familiar with working with threads or just starting out?
>>
>>Here are two basic thread concepts:
>>- Only one event loop per thread.
>>- Threads stop themselves. They can't be stopped directly from outside; an
>>
>>outsider has to send them a message to stop.
>>
>>I think of a thread as a stand-alone program that is responsible for
>>opening
>>and closing data and managing I/O, but it is different only in the fact
>>that
>>it may be one of several such programs.
>>
>>In your situation, if nothing happens in one thread for 15 minutes, you
>>want
>>it to shut down the whole program. If it were a stand-alone program it
>>would
>>do so. But in your case, it is just one of several programs running. The
>>other programs may be active and not want to shut down. It's like several
>>children in one car; some want to stop, others want to go on. Dad or Mom
>>decides when everyone will stop.
>>
>>In your app., each thread needs to tell Dad it is ready to stop. When Dad
>>gets "ready-to-stop" messages from all the children, Dad tells each one
>>it's
>>now OK to stop. They do so and report back after they have stopped. When
>>Dad
>>sees all the children have stopped, he pulls over and stops the car.
>>
>>In your main thread (Dad) establish a "ready-to-stop" status board that
>>hold
>>the status for each thread (Child). When there is activity in a thread, it
>>
>>sets it's status as "not-ready." If 15 minutes go by without any activity,
>>
>>the thread changes its status to "ready." You need a loop in the main
>>program that continuously monitors the "ready-to-stop" status board. When
>>all threads are in a "ready-to-stop" mode, the main program sends
> terminate
>>
>>messages to all the threads and waits until all threads report back they
>>are
>>terminated. Then the main program cleans up itself and quits.
>>
>>You may not have thought about this yet, but you might want to do so. You
>>may want to stop your app via an external method. Pressing alt-C may not
>>effect an orderly shutdown of all threads. You may leave connections to
>>databases open. For an external trigger I use the presence of a file,
>>EXITNOW.TXT. When the file is present the main monitoring loop tells each
>>thread to stop. When all threads have stopped, the main program shuts
> down.
>>
>>You could add this feature to your loop that monitors the "ready-to-stop"
>>status board.
>>
>>How about putting together a sample program that implements these concepts
>>
>>and show us the code. Then you can ask us specific questions about things
>>that give you difficulty.
>>
>>Tim
>
>
Don SchmitzRe: Multiple Threads
on Tue, 03 Oct 2006 09:04:25 -0500
Tim, 
Attached is the Test program I wrote that I'm trying to use a thread to read
the event queue to determine if the user is still actively doing something. 
I would like to deploy this in text mode and it is my understanding that
there is only one event queue to deal with while in text or hybrid mode.  I
have been able to make the program work to some degree by using appevent /
postappevent but that fails when a mouse click is issued and I wouldn't
think I would have to read and remove and then add back the event.  I just
want to monitor it.  My ultimate goal is to monitor keyboard / mouse
activity and reset a timer for say 15 minutes and if after 15 miniutes no
activity then close down all programs.


*********************************************
Don Schmitz
University Hospital Surgery Dept.
600 Highland Ave. F8-164
Madison, WI  53792
(608) 263-9307
Email: Dons@surgery.wisc.edu



>>> Tim Snyder<timothy.snyder@nreis.com> 10/3/2006 7:22:14 AM >>>
>don,
>
>I can't open .prg files. Try zipping it first.
>Thanks,
>Tim
>
>"Don Schmitz" <DONS@surgery.wisc.edu> wrote in message 
>news:616f9a17$1a672821$b1c63@news.alaska-software.com...
>> Again thanks for the info it was helpful.
>>
>> Yes I'm new to Xbase and to Threads.....
>> I have included a test program which is my first attempt which doesn't 
>> seem
>> to be honoring the key movement that you would hit in the read
>statements.
>> I'm sure I'm doing something wrong, if you can help it would be 
>> appreciated.
>> I did take a look at the listserver thread from the other comment but
>the
>> end result was over my head.
>>
>>
>> *********************************************
>> Don Schmitz
>> University Hospital Surgery Dept.
>> 600 Highland Ave. F8-164
>> Madison, WI  53792
>> (608) 263-9307
>> Email: Dons@surgery.wisc.edu 
>>
>>
>>
>>>>> Tim Snyder<timothy.snyder@nreis.com> 9/26/2006 7:58 AM >>>
>>>Hi Don,
>>>
>>>Perhaps you could explain for us why the earlier solutions didn't work
>for
>>>
>>>you. There was a lot of good information in those posts. Are you very
>>>familiar with working with threads or just starting out?
>>>
>>>Here are two basic thread concepts:
>>>- Only one event loop per thread.
>>>- Threads stop themselves. They can't be stopped directly from outside;
>an
>>>
>>>outsider has to send them a message to stop.
>>>
>>>I think of a thread as a stand-alone program that is responsible for
>>>opening
>>>and closing data and managing I/O, but it is different only in the fact
>>>that
>>>it may be one of several such programs.
>>>
>>>In your situation, if nothing happens in one thread for 15 minutes, you
>>>want
>>>it to shut down the whole program. If it were a stand-alone program it
>>>would
>>>do so. But in your case, it is just one of several programs running. The
>>>other programs may be active and not want to shut down. It's like
>several
>>>children in one car; some want to stop, others want to go on. Dad or Mom
>>>decides when everyone will stop.
>>>
>>>In your app., each thread needs to tell Dad it is ready to stop. When
>Dad
>>>gets "ready-to-stop" messages from all the children, Dad tells each one
>>>it's
>>>now OK to stop. They do so and report back after they have stopped. When
>>>Dad
>>>sees all the children have stopped, he pulls over and stops the car.
>>>
>>>In your main thread (Dad) establish a "ready-to-stop" status board that
>>>hold
>>>the status for each thread (Child). When there is activity in a thread,
>it
>>>
>>>sets it's status as "not-ready." If 15 minutes go by without any
>activity,
>>>
>>>the thread changes its status to "ready." You need a loop in the main
>>>program that continuously monitors the "ready-to-stop" status board.
>When
>>>all threads are in a "ready-to-stop" mode, the main program sends
>> terminate
>>>
>>>messages to all the threads and waits until all threads report back they
>>>are
>>>terminated. Then the main program cleans up itself and quits.
>>>
>>>You may not have thought about this yet, but you might want to do so.
>You
>>>may want to stop your app via an external method. Pressing alt-C may not
>>>effect an orderly shutdown of all threads. You may leave connections to
>>>databases open. For an external trigger I use the presence of a file,
>>>EXITNOW.TXT. When the file is present the main monitoring loop tells
>each
>>>thread to stop. When all threads have stopped, the main program shuts
>> down.
>>>
>>>You could add this feature to your loop that monitors the
>"ready-to-stop"
>>>status board.
>>>
>>>How about putting together a sample program that implements these
>concepts
>>>
>>>and show us the code. Then you can ask us specific questions about
>things
>>>that give you difficulty.
>>>
>>>Tim
>>
>> 



TEST.ZIP
Tim SnyderRe: Multiple Threads
on Tue, 03 Oct 2006 17:17:36 -0400
Don,

Take another look at what I first wrote and the help entries for AppEvent() 
and LastAppEvent().

When you started the thread with CHK_TIME(), you created a second event 
queue. Your calls to LastAppEvent() are looking at the events of the thread 
running CHK_TIME(), not the events of the main thread with the GET/READ.

Try working with the code in the main thread to determine if you can detect 
a period of time when there are no events. Once you can do that, then you 
can stop the program with a simple IF elapsed time is more than x seconds, 
close down. You could do this without a second thread.

Tim
Don SchmitzRe: Multiple Threads
on Wed, 04 Oct 2006 11:57:27 -0500
I thought when you compile in Text mode there is only one event Queue.  I
have seen this listed a number of times in the Help entries.

I understand what your saying with trying to make this work in the main
thread but this was just a test program when I use it in real life it would
be much harder to implement without the second thread do to may other loops
and procedures so I thought the best way would be to create a second thread
to monitor this, I'm now using nextappevent(,,Threadid()) and seems to be
working.
Is it not true that polling lastappevent() would always report something
after the first appevent happened since even appevent will be waiting for
the next event but lastappevent will report the last event that took place
or does it clear it out at some point.  I guess it would it appevent has a
timeout on it....maybe I just answered my own question.


*********************************************
Don Schmitz
University Hospital Surgery Dept.
600 Highland Ave. F8-164
Madison, WI  53792
(608) 263-9307
Email: Dons@surgery.wisc.edu



>>> Tim Snyder<timothy.snyder@nreis.com> 10/3/2006 4:17:36 PM >>>
>Don,
>
>Take another look at what I first wrote and the help entries for
AppEvent()
>
>and LastAppEvent().
>
>When you started the thread with CHK_TIME(), you created a second event 
>queue. Your calls to LastAppEvent() are looking at the events of the
thread
>
>running CHK_TIME(), not the events of the main thread with the GET/READ.
>
>Try working with the code in the main thread to determine if you can
detect
>
>a period of time when there are no events. Once you can do that, then you 
>can stop the program with a simple IF elapsed time is more than x seconds,
>
>close down. You could do this without a second thread.
>
>Tim
Tim SnyderRe: Multiple Threads
on Mon, 09 Oct 2006 13:25:14 -0400
Don,

>I thought when you compile in Text mode there is only one event Queue.  I
> have seen this listed a number of times in the Help entries.

Yes, the text of PostAppEvent() says:
If a program is not linked with the linker switch /PM:PM, it is a text mode 
application which has only one event queue. When a text mode application 
uses multiple threads, they share this queue.

However, that's not exactly correct. Take a look at the attched code.
The events happen in the main thread. nEvent in the Chk_time thread is 
always 0

> Is it not true that polling lastappevent() would always report something
> after the first appevent happened since even appevent will be waiting for
> the next event but lastappevent will report the last event that took place
> or does it clear it out at some point.  I guess it would it appevent has a
> timeout on it....maybe I just answered my own question.

LastAppevent() always reports the last event and that makes using it 
cumbersome. For instance, if the "t" key is pressed twice, the last appevent 
is the same after each key, masking the fact that an event happened. Also, 
if there is no activity after the "t" key is pressed, the lastappevent is 
unchanged.

Read the comments and study the code I wrote. It takes a lot of work to 
determine if an event happened using your architiecture.
The code I came up with is very specific for the GET system. You would have 
to expand it to cover MENU TO, INKEY(), ACHOICE(), MEMOEDIT(), etc.

I would turn your architecture on its head and put the GETs in the thread 
and keep the monitoring loop in the main thread. Let the GET loop decide if 
it has activity and if not, set lQuitNow to .T. In the Main Thread you have 
a very simple:
lQuitNow := .F.
DO While .NOT. lQuitNow
  sleep(100)
ENDDO
QUIT

Then you just have to modify the GET system (in GetSysX.prg), and other wait 
states to set or reset lQuitNow.

Tim 




TEST.ZIP
AUGE_OHRRe: Multiple Threads
on Tue, 26 Sep 2006 20:20:30 +0200
hi,

> I'm looking for a way to control users of a program that if they have not
> done any work in the application for say 15 minutes in the app that it
would
> shut the app down.

Xbase++ Hybrid or full GUI ?

not shure if you meen "timeout" main application ?
if yes, have a look at "Timeout" in "public.xbase++.generic" from 19.09.2006

greetings by OHR
Jimmy
Don SchmitzRe: Multiple Threads
on Thu, 28 Sep 2006 11:34:29 -0500
Hybrid for now...


*********************************************
Don Schmitz
University Hospital Surgery Dept.
600 Highland Ave. F8-164
Madison, WI  53792
(608) 263-9307
Email: Dons@surgery.wisc.edu



>>> AUGE_OHR<AUGE_OHR*AT*CSI.COM> 9/26/2006 1:20 PM >>>
>hi,
>
>> I'm looking for a way to control users of a program that if they have
>not
>> done any work in the application for say 15 minutes in the app that it
>would
>> shut the app down.
>
>Xbase++ Hybrid or full GUI ?
>
>not shure if you meen "timeout" main application ?
>if yes, have a look at "Timeout" in "public.xbase++.generic" from
>19.09.2006
>
>greetings by OHR
>Jimmy
AUGE_OHRRe: Multiple Threads
on Wed, 04 Oct 2006 07:12:04 +0200
hi,

> Hybrid for now...

i did have a look at your sample. If you like to have a "timeout"
in your GET than you are going the wrong way.

when using SETMOUSE( .T. ) make a copy of
C:\ALASKA\XPPW32\SOURCE\SYS\GetSysX.prg
and modify AppEvent( @mp1, @mp2, @obj , 0 ) 4st. parameter
for "timeout".

greetings by OHR
Jimmy
Don SchmitzRe: Multiple Threads
on Wed, 04 Oct 2006 11:58:02 -0500
Thanks for the tip .... I'll take a look at that.


*********************************************
Don Schmitz
University Hospital Surgery Dept.
600 Highland Ave. F8-164
Madison, WI  53792
(608) 263-9307
Email: Dons@surgery.wisc.edu



>>> AUGE_OHR<AUGE_OHR*AT*CSI.COM> 10/4/2006 12:12:04 AM >>>
>hi,
>
>> Hybrid for now...
>
>i did have a look at your sample. If you like to have a "timeout"
>in your GET than you are going the wrong way.
>
>when using SETMOUSE( .T. ) make a copy of
>C:\ALASKA\XPPW32\SOURCE\SYS\GetSysX.prg
>and modify AppEvent( @mp1, @mp2, @obj , 0 ) 4st. parameter
>for "timeout".
>
>greetings by OHR
>Jimmy