• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

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
0
kwang080897
Asked:
kwang080897
  • 4
  • 3
1 Solution
 
kvjCommented:
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:

    ButtonPressed:=False;
    while (not ButtonPressed) and (not FileExists(..)) do
      Application.ProcesMessages;

I have something similar in a spell cheking component
           
0
 
interCommented:
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:

type
  TMyThread = class(TThread)
      procedure Execute;
  end;

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

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

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

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
0
 
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

Oki...

function Tusers.waitforfile(filename:string):boolean; // Returns true if file has been createt
 var
  tekst:string;
  f: textfile;
  S: longint;
  count:integer;
 begin
   Application.CreateForm(Tventform, ventform);
   ventform.show;
   ventform.label1.Refresh;
   ventform.Animate1.Play(1,23,0);
   count:=0;
   assignfile(f,filename);
   s:=0;
     try
      if not fileexists(filename) then tekst:='Awaiting file'
      else tekst:='Writing';
       if tekst<>ventform.label1.Caption then begin;
        ventform.label1.Caption:=tekst;
        ventform.label1.Refresh;
       end;
      reset(f);
      s:=s+1;
     except
      count:=count+1;
     end;
     if ventform.label1.tag=100 then s:=1;
    until s>0;
   sleep(50);
   closefile(f);
   sleep(50);
   result:=true;
   ventform.Label1.caption:='';
   ventform.Destroy;
  end;

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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
interCommented:
Sorry, the
(not KeyPressed and Application.Terminated)
should be
(not KeyPressed and not Application.Terminated)
0
 
interCommented:
Hi,
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?
Igor
0
 
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
0
 
interCommented:
Thanks friend,
I ll be here for anything...(anyway I may not deserve the points)
Regards, Igor
0
 
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


0
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.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now