Processmessages Without application.processmessages

I need to make a window that stays open until a certain file exists. This i got right, but i would like to be abble to break this if something goes wrong (break whith a keystroke or even better a Press to a button). But what i have is a loop that await this file, and i can't use application.processmessages, becaurse this takes me out of the loop and i don't want that
Who is Participating?
interConnect With a Mentor Commented:
Thanks friend,
I ll be here for anything...(anyway I may not deserve the points)
Regards, Igor
I would make a form with a cancel button. When the cancel button is pressed
you set a variable ButtonPressed:=True;

in the forms onActivate event you write the following:

    while (not ButtonPressed) and (not FileExists(..)) do

I have something similar in a spell cheking component
We'd better create a Thread from withing your main form an in that thread check for the file. By this way you never need to get out of the thread. Here is how to declere a simple thread:

  TMyThread = class(TThread)
      procedure Execute;

procedure TMyThread.Execute;
   // Show you form here
   //Keypressed is global variable and you set it to true from your main form
   // when needed
   while (not KeyPressed and Application.Terminated) do
       //do your checking here
       // common for all threads for not raising CPU cycles

From your main form(may be with a button do the following;

  Mt : TMyThread;
   Mt := TThread.Create(true); //create suspended
   Mt.FreeOnTerminate := true;
   KeyPressed := false;

There should be a global boolean variable KeyPresses and you should check in one of your forms and set to TRUE. When it is set to true your thread is automatically terminated

Regards, Igor
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

kwang080897Author Commented:
Unfortunately I Couldent get any of the examples to work, soo i'l trie to explaint what I'm doing....

Inter>> I hav never worked with a Thread before, so i didden't know hpw to set it up, but as said early'er i will give some more details


function Tusers.waitforfile(filename:string):boolean; // Returns true if file has been createt
  f: textfile;
  S: longint;
   Application.CreateForm(Tventform, ventform);;
      if not fileexists(filename) then tekst:='Awaiting file'
      else tekst:='Writing';
       if tekst<>ventform.label1.Caption then begin;
     if ventform.label1.tag=100 then s:=1;
    until s>0;

This is my function to check... if file exists... and when the file is done being written to....

The Ventform(Would be Waitform in english) Shows up and dissapear when file is done written, on my form i have a button, but I'ts not possible to press this button, because i'm running in a loop. I want to run in a loop, because the following code can't do anything whithow the file that has been written.... Only if something chould go wrong, I'l like to be able to get out of my loop... Preferable whit a press on the botton on the "Ventform"

Inter>> Yous might work, but i wouldent arsk futher questions till i was sure you know what i want to do

Thank you in advance
Sorry, the
(not KeyPressed and Application.Terminated)
should be
(not KeyPressed and not Application.Terminated)
I think I could understand the problem. You have a code to execute after a certain file appers on the disk. But if there is a problem you do not want to execute it.
Lets think of another way: your application is a single process application at the begining, so without PeekMessage (or Application.ProcessMessages which does the same thing easily) you have no chance to transfer control to the windows. So we have several solutions:
- First of all, the Application.ProcessMessages does not take you out of the loop it just goes to windows and returns the its older execution points. I can not understand the problem with processmessages.
- As a second solution we can safely do it with threads. It is easy man, and it gives you great power to learn threading mechanism.
- Third solution is a kind of SIGNAL or TRIGGER mechanism. Mainly we set up a new Windows message and use PostMessage if  our loop be succesfull.(this is the solution requires more lines of code and needs an understanding of windows internal messaging)
So, the code above tries to access a file exculusively, and I think it can do it(do not miss REPEAT statement, there is an orphan UNTIL above)

So, which way do you prefer?
kwang080897Author Commented:
Hi Igor !!

I Think that i've solved the problem by using Processmessages, i don't know what i did Wrong, but i seems to work all right now

I am sorry for the trouble, i'm would like to look at treads someday, so i might just get back to you....

Please post an answer to me, an i will grade your answer(don't think a can with a comment), You deserved it .... Thanks a lot
kwang080897Author Commented:
Hi Igor !!

Of course you did, you helped me looking the right way, so dont feel bad about it....

By the way..... I don't need all those Points anyway, so i'm greatfull for a fast and effective Responce

Thanks again

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.