Solved

Processmessages Without application.processmessages

Posted on 1998-08-25
8
190 Views
Last Modified: 2010-04-04
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
Comment
Question by:kwang080897
  • 4
  • 3
8 Comments
 

Expert Comment

by:kvj
Comment Utility
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
 
LVL 5

Expert Comment

by:inter
Comment Utility
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
 

Author Comment

by:kwang080897
Comment Utility
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
 
LVL 5

Expert Comment

by:inter
Comment Utility
Sorry, the
(not KeyPressed and Application.Terminated)
should be
(not KeyPressed and not Application.Terminated)
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:inter
Comment Utility
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
 

Author Comment

by:kwang080897
Comment Utility
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
 
LVL 5

Accepted Solution

by:
inter earned 100 total points
Comment Utility
Thanks friend,
I ll be here for anything...(anyway I may not deserve the points)
Regards, Igor
0
 

Author Comment

by:kwang080897
Comment Utility
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now