Recent

Author Topic: know when a procedure is complete  (Read 3482 times)

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: know when a procedure is complete
« Reply #15 on: April 23, 2020, 07:42:05 pm »
I didn't remember exactly. I wrote some graphics functions maybe my own OpenGL GUI. I need some special effect. It needs to be run after certain object initialized, and they must run sequentially. I do not familiar with threads, I used events and TTimer (created runtime) heavily. My tricks worked as what I want. The key is, it must set a global variable when the task is finished.

What I can imagine now, for example in game programming. If the player (spaceship) drop a bomb, then I will send tasks like this:
1. Initialize the bomb
2. The bomb travels until it hits an object or ground
3. Explode
Events and ttimers is a pseudo asynchronous (or semi asynchronous?) environment. TTimer will add a message in the message queue of the application when it fires, as long as your application is in the loop of message processing it will process each message/event in the order they are received.
It is still a single thread executing in a linear manner it just gives the illusion of parallel execution just like a cothreads library in a single core processor would.
Global variables will help as long as they are atomic operations ee booleans or properly aligned 16, 32 bit etc set/get operations.
The moment you jump to something a bit more complex ee strings, arrays etc you will end up with a lot of problems with out a proper synchronization primitive (semaphore, mutex, critical sections or what ever they are called in the framework you are using, eg fpc has criticalsections instead of mutex)


Handoko

  • Hero Member
  • *****
  • Posts: 5524
  • My goal: build my own game engine using Lazarus
Re: know when a procedure is complete
« Reply #16 on: April 23, 2020, 08:05:12 pm »
I knew that wasn't the best solution. I do not have any programming education and I miss many programming topics, I write code based on what I ever read and trial and error. I knew what thread and semaphore are. That problem I got can simply solved using TTimer and a global variable why should I care to use thread and semaphore which I hadn't really explored.

Don't focus on me, the OP is the one who needs help.

440bx

  • Hero Member
  • *****
  • Posts: 6157
Re: know when a procedure is complete
« Reply #17 on: April 23, 2020, 08:07:28 pm »
Thanks for your answers, what I am trying to know is when the procedure "my_tasks1" has finished its operations so that we can proceed to the next procedure.
What happened to me is when I put them like this:

procedure TForm1.Button1Click (Sender: TObject);
begin
        my_tasks1; // first procedure
        my_tasks2; // second procedure
        my_tasks3; // third procedure
end;

I noticed that the 3 procedures were running fast almost at the same time, which sends me errors in my application.

So how do I know when a procedure finishes its tasks?
The way you describe the problem is ambiguous.  Are those "my_tasksX" processes or threads ?

Presuming threads to keep things simple, one simple way to do what you want is to create all three threads in a suspended state.  When you create a thread, you get to pass a parameter to it which  provides information you want the thread to have.  What you do is pass to one thread the thread handle of the next thread you want to be active once that one ends.  That way, just before the thread calls ExitThread (in the case of Windows) it does a "ResumeThread" to activate the next thread.

That way, you don't need any synchronization objects and the threads take care of everything.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Ericktux

  • Sr. Member
  • ****
  • Posts: 384
    • ericksystem software
Re: know when a procedure is complete
« Reply #18 on: April 23, 2020, 10:28:55 pm »
Hello friends, they are normal procedures, they are not threads.

I love desktop software
https://www.ericksystem.com

jamie

  • Hero Member
  • *****
  • Posts: 7607
Re: know when a procedure is complete
« Reply #19 on: April 23, 2020, 11:02:32 pm »
The procedure will complete when its done...
it will not return from where you called it until it itself returns..

 The only issue you may have is if you call the Application.ProcessMessages and this
main block was some sort of user button event to get the ball a rolling...
 
 If you are not using this call then the procedure will not return until it is complete and thus the next one will execute the order that you have it...

 if you are showing some form within the procedures then yes they will return with the form still showing so to avoid that you show the form as MODAL...

etc

 Maybe you have the wrong impression as to how code execution happens..

 Report back when you need more details.

The only true wisdom is knowing you know nothing

Ericktux

  • Sr. Member
  • ****
  • Posts: 384
    • ericksystem software
Re: know when a procedure is complete
« Reply #20 on: April 26, 2020, 05:55:10 am »
Hello friends, my main problem was that these three procedures run very fast and produced errors.
At the moment I'm using it like this and I'm doing well at the moment:

Code: Pascal  [Select][+][-]
  1. Uses
  2. Windows, . . . . ;
  3.  
  4. procedure Delay( const msecs:integer);
  5. var
  6. FirstTickCount:longint;
  7. begin
  8. FirstTickCount:=GetTickCount;
  9. repeat
  10. Application.ProcessMessages;
  11. until
  12. ((GetTickCount-FirstTickCount) >= Longint(msecs));
  13. end;
  14.  
  15. procedure TForm1.Button1Click(Sender: TObject);
  16. begin
  17.      my_tasks1; // first procedure
  18.      delay(2000); //delay 2 seconds, my app will not freeze
  19.           begin
  20.           my_tasks2; // second procedure
  21.           delay(2000); //delay 2 seconds, my app will not freeze
  22.           end;
  23.               begin
  24.               my_tasks3; // third procedure
  25.               end;
  26. end;


I will comment on any news  :)
I love desktop software
https://www.ericksystem.com

Thaddy

  • Hero Member
  • *****
  • Posts: 18792
  • Glad to be alive.
Re: know when a procedure is complete
« Reply #21 on: April 26, 2020, 08:31:24 am »
sequentially run procedures that do not use threads - you wrote that - can not directly interact with each other so can not cause speed problems. The delays are bogus.
You need to debug the code in another way.
Are the procedures using shared or global variables? in that case examine those variables first.
« Last Edit: April 26, 2020, 08:34:01 am by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

 

TinyPortal © 2005-2018