?
Solved

Processmessages Without application.processmessages

Posted on 1998-08-25
8
Medium Priority
?
219 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
ID: 1337644
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
ID: 1337645
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
ID: 1337646
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:inter
ID: 1337647
Sorry, the
(not KeyPressed and Application.Terminated)
should be
(not KeyPressed and not Application.Terminated)
0
 
LVL 5

Expert Comment

by:inter
ID: 1337648
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
ID: 1337649
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 400 total points
ID: 1337650
Thanks friend,
I ll be here for anything...(anyway I may not deserve the points)
Regards, Igor
0
 

Author Comment

by:kwang080897
ID: 1337651
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

839 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