Solved

Processmessages Without application.processmessages

Posted on 1998-08-25
8
192 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
 
LVL 5

Expert Comment

by:inter
ID: 1337647
Sorry, the
(not KeyPressed and Application.Terminated)
should be
(not KeyPressed and not Application.Terminated)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

25 Experts available now in Live!

Get 1:1 Help Now