Recent

Author Topic: Form freeze until procedure done  (Read 8963 times)

zoiddani

  • New Member
  • *
  • Posts: 29
Form freeze until procedure done
« on: February 06, 2011, 04:57:12 pm »
Hi,
I've made quite much little applications, and I have a something which I don't like.
If i put a procedure in the buttons click event. then when i click on the  button the whole form will freeze until the procedure's done. Until now, that wasnt a problem, because my programs were fast, but now i made one, which can run a process for minutes. How can I solve this freeze?
Thank you!

Dibo

  • Hero Member
  • *****
  • Posts: 1057
Re: Form freeze until procedure done
« Reply #1 on: February 06, 2011, 04:58:58 pm »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12010
  • Debugger - SynEdit - and more
    • wiki
Re: Form freeze until procedure done
« Reply #2 on: February 06, 2011, 05:29:36 pm »
put Application.ProcessMessages; into your procedure.

BUT, be aware. This also means that the user can click the button/menu whatever, while your procedure is already running...

Whenever anything happens (button, pressed, form moved, form needs painting, ... an event is send to your app. The events are processed one after the other.

So your button.OnClick is such an event, and while it runs, all others will wait.
"Application.ProcessMessages;" will process those waiting ones.
so if you call it regularly then your form will still react.

But as i said the button can be pressed again.
  Button.OnClick:
    Your code running
    ProcessMessages
       Button.OnClick (nested call)
    Your code from the outer onclick continues

Most often you do not want that to happen
You can either set a variable, and check at the begin of your OnClick. Or you set the buttons Enabled to False
« Last Edit: February 06, 2011, 05:35:13 pm by Martin_fr »

zoiddani

  • New Member
  • *
  • Posts: 29
Re: Form freeze until procedure done
« Reply #3 on: February 06, 2011, 06:40:16 pm »
Edit: Ok, I've rewritten a part of the code with the use of a thread. Now, the form still freezes. Is this normal, or have I done something wrong?
« Last Edit: February 06, 2011, 09:05:43 pm by zoiddani »

zoiddani

  • New Member
  • *
  • Posts: 29
Re: Form freeze until procedure done
« Reply #4 on: February 08, 2011, 06:56:18 pm »
I stopped using threads because it's complexity.
Now I encountered the following problem, didn't wanted to open a new thread:
After running a procedure sometimes I get the SIGSEGV error from the app. I get it not randomly, just at some types. It's a really complicated procedure, I won't post it here, but I've made a watching procedure, which writes to a file at some points, and I saw, that the procedure runs fine, after that I get the SIGSEGV error. How is this possible?

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: Form freeze until procedure done
« Reply #5 on: February 08, 2011, 08:20:03 pm »
I stopped using threads because it's complexity.
Now I encountered the following problem, didn't wanted to open a new thread:
After running a procedure sometimes I get the SIGSEGV error from the app. I get it not randomly, just at some types. It's a really complicated procedure, I won't post it here, but I've made a watching procedure, which writes to a file at some points, and I saw, that the procedure runs fine, after that I get the SIGSEGV error. How is this possible?
How does your watcher access the worker procedure?  how does your worker procedure release it's hold on the CPU so your watcher can run?

When I've done this sort of thing in the past, I've used a timer as the driver for a state engine.  each onTimer function controls the timer and sets the next state just by changing the onTimer function.  this works if you are waiting around for hardware or have a sequence that needs delays.  just remeber to disable the timer at the start of each OnTimer funtion so that only one is in the que at a time or make sure the time interval is longer than the time it takes the funtion to run.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

circular

  • Hero Member
  • *****
  • Posts: 4462
    • Personal webpage
Re: Form freeze until procedure done
« Reply #6 on: February 08, 2011, 10:12:41 pm »
put Application.ProcessMessages; into your procedure.
Yep, that's the simplest way. While you do that you must disable the components that must not be touched during the process. For example, if the procedure starts when clicking the button, then, at the beggining of the procedure, disable the button. At the end, enable it again.
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018