?
Solved

How to case my case statement and implement app.processmessages or exceptionhandling if necessary?

Posted on 2005-04-26
9
Medium Priority
?
240 Views
Last Modified: 2010-04-06
Hi Folks!

First of all many many thanks fly out to all of ya out here for assisting me the last couple of weeks! I 'launched' my project yesterday and received nothing but compliments and apprecation from the person whom I created it for!

Now off to make the title of this question legitimate :)

Check this piece of code as well as my comments please. This button gets clicked over and over again until I direct the pagecontrol to 'walk' to another tabsheet. I put several questions as comments or better said assumptions of mine next to it. The reason I want to implement application.processmessages is because of the fact some people easily took more than half an hour to click through this button thingie and my program simply seems to keep up eating resources when it runs for such a long period. emo1 is an integer I put under the forms declaration to use it all throught the program. This seems to work okay.


// here goes
procedure TForm1.BTNEMO1Click(Sender: TObject);
begin
  emo1 := emo1 + 1;                                                   //  Inc(emo1)
  LBEMO1.Items.Add(InttoStr(TBEMO1.Value));
  TBEMO1.Value := 4;
  case emo1 of    
    0:                   // this will never be the case, these are just the initial values already present at start so it should skip this one
      begin
        LBLEMOTION1.Caption := '';
        LBLE1LEFT.Caption := 'Ontevreden';
        LBLE1RIGHT.Caption := 'Tevreden';
        BTNEMO1.Enabled := False;
      end;
    1:
      begin
        LBLEMOTION1.Caption := '';
        LBLE1LEFT.Caption := 'Schaamte';
        LBLE1RIGHT.Caption := 'Trots';
        BTNEMO1.Enabled := False;
      end;
    2:
      begin
        LBLEMOTION1.Caption := 'Blij';
        LBLE1LEFT.Caption := 'Nee';
        LBLE1RIGHT.Caption := 'Ja';
        BTNEMO1.Enabled := False;
      end;
    3:
      begin
        LBLEMOTION1.Caption := 'Verdrietig';
        LBLE1LEFT.Caption := 'Nee';
        LBLE1RIGHT.Caption := 'Ja';
        BTNEMO1.Enabled := False;
      end;
    4:
      begin
        LBLEMOTION1.Caption := 'Boos';
        LBLE1LEFT.Caption := 'Nee';
        LBLE1RIGHT.Caption := 'Ja';
        BTNEMO1.Enabled := False;
      end;
    5:
      begin
        LBLEMOTION1.Caption := 'Bang';
        LBLE1LEFT.Caption := 'Nee';
        LBLE1RIGHT.Caption := 'Ja';
        BTNEMO1.Enabled := False;
      end;
    6:                                                                                  // max emo = 6 right? given just the buttonclicks after another?
      begin
        emo1 := 0;                                                                // emo becomes 0
        PC1.ActivePageIndex := PC1.ActivePageIndex + rnd;      // can I simply 'jump' out of this procedure this way ???
        LBLEMOTION1.Caption := '';
        LBLE1LEFT.Caption := 'Ontevreden';
        LBLE1RIGHT.Caption := 'Tevreden';
        BTNEMO1.Enabled := False;
        Application.ProcessMessages;                                      // is this the right place to put an application.processmessages ???
     end;
    else
      begin
        Raise exception.Create('Fout opgetreden');          // will this catch all exceptions that could occur in this case statement ???
      end;
  end;
  Application.ProcessMessages;                                             // is this the right place to put an application.processmessages ???
end;

// this is it...



Some background Info for those interested. Feel free to skip it, it doesnt contribute to this question or answer.

I'm using several case statements in my program and after some checking of some values I wasn't quite so sure anymore whether I was 'casing' the right way. I implemented a lot of ShowMessage(IntToStr(ingiesthingies)) to learn a little about all possible changes but I still easily get confused figuring this whole thing out.
The situation even gets a little complexer here because of the fact I read quite some pages of pure Delphi stuff regarding application.processmessages and exception handling. I must admit I never even knew of application.processmessages and exception handling always seemed way out of my league to simply start using it. Now I've come to conclude that I 'handled' any possible exceptions by simply popping up a message box and I really never bump into those strange EDivideOnZero thingies and stuff but bear with me a lot could very well pass me by without me noticing it. My projects never really use very complex code because I really want to know what the H*ll I'm actually doing and I only allow some portion of ABRACADABRA-Code coming from you guys and when I'm lucky ah well then it's commented well otherwise it isn't commented at all and I simply use it without touching the concept.


Regards Peter

Please comment any yes's or no's so I know why I shouldn't do or should do whatever, okay?
0
Comment
Question by:PeterdeB
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 500 total points
ID: 13866617
Application.ProcessMessages can be put in many parts of the system, but definitely within loops so that it lets the computer "see" that the application is busy and has not just locked for example.

As for the exception part you need to wrap the whole thing in an exception

procedure TForm1.BTNEMO1Click(Sender: TObject);
begin
  emo1 := emo1 + 1;                                                   //  Inc(emo1)
  LBEMO1.Items.Add(InttoStr(TBEMO1.Value));
  TBEMO1.Value := 4;
  Application.ProcessMessages; // I would put here if you think you need it
  try
    case emo1 of
      0:                   // this will never be the case, these are just the initial values already present at start so it should skip this one
        begin
          LBLEMOTION1.Caption := '';
          LBLE1LEFT.Caption := 'Ontevreden';
          LBLE1RIGHT.Caption := 'Tevreden';
          BTNEMO1.Enabled := False;
        end;
      1:
        begin
          LBLEMOTION1.Caption := '';
          LBLE1LEFT.Caption := 'Schaamte';
          LBLE1RIGHT.Caption := 'Trots';
          BTNEMO1.Enabled := False;
        end;
      2:
        begin
          LBLEMOTION1.Caption := 'Blij';
          LBLE1LEFT.Caption := 'Nee';
          LBLE1RIGHT.Caption := 'Ja';
          BTNEMO1.Enabled := False;
        end;
      3:
        begin
          LBLEMOTION1.Caption := 'Verdrietig';
          LBLE1LEFT.Caption := 'Nee';
          LBLE1RIGHT.Caption := 'Ja';
          BTNEMO1.Enabled := False;
        end;
      4:
        begin
          LBLEMOTION1.Caption := 'Boos';
          LBLE1LEFT.Caption := 'Nee';
          LBLE1RIGHT.Caption := 'Ja';
          BTNEMO1.Enabled := False;
        end;
      5:
        begin
          LBLEMOTION1.Caption := 'Bang';
          LBLE1LEFT.Caption := 'Nee';
          LBLE1RIGHT.Caption := 'Ja';
          BTNEMO1.Enabled := False;
        end;
      6:                                                                                  // max emo = 6 right? given just the buttonclicks after another?
        begin
          emo1 := 0;                                                                // emo becomes 0
          PC1.ActivePageIndex := PC1.ActivePageIndex + rnd;      // can I simply 'jump' out of this procedure this way ???
          LBLEMOTION1.Caption := '';
          LBLE1LEFT.Caption := 'Ontevreden';
          LBLE1RIGHT.Caption := 'Tevreden';
          BTNEMO1.Enabled := False;
          Application.ProcessMessages;                                      // is this the right place to put an application.processmessages ???
       end;
    end;
  except
    on e: Exception do
    begin
      // write code here for what you want to do
    end;
  end;
end;
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13866668
To jump out of a method, ie. procedure or function you just need to call "exit"

Because you are trying to catch any errors in the case statement, you need to wrap the whole thing in an exception handler.
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13866687
O and as a personal opinion, comment as much as you can.
You cannot have enough information written down to be honest.
Expecially when you find you have to go look at the code again after a while, you always seem to forget something.
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 17

Expert Comment

by:geobul
ID: 13866757
Hi,

> Inc(emo1)
Logically it is the same but IMHO Inc(emo1) will be slower because you're calling a function, passing a parameter to it and that function actually does the same in its body.

> this will never be the case
Agree

> max emo = 6 right?
Yes, it seems like that. Unless some other code changes the variable.

> can I simply 'jump' out of this procedure this way ???
Don't understand...

> will this catch all exceptions that could occur in this case statement ???
No. It will raise an exception if emo1 is out of the interval [0..6], i.e 7, for instance.

For an exception handling I would use something like:

procedure TForm1.BTNEMO1Click(Sender: TObject);
begin
 try
  emo1 := emo1 + 1;
  ...
  else
      begin
        Raise exception.Create('Fout opgetreden');         end;
  end; // end of case statement
 except
   // do something here with the exception like
   on E: Exception do ...
 end;
end;

> is this the right place to put an application.processmessages
I don't think you need to call it at all. The following:

LBEMO1.Items.Add(InttoStr(TBEMO1.Value));

seems to be the line that is 'eating' your resources.

Regards, Geo
0
 

Author Comment

by:PeterdeB
ID: 13866824
Hi MikeLittleWood!

Geez thanks for your rapid response, a whole lot of useful information. The points are yours!

Regards Peter
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13866841
I agree with Geo regarding the application.processmessages. It probably isnt needed within the method, but bear it in mind when doing loops.
0
 

Author Comment

by:PeterdeB
ID: 13867496
Geobul,

And I have run into the refreshing thingie of my browser once again cause at time of posting only MikeLittleWood had responded and none of Geobuls answers showed up....hmmmz.......As always your answers are just as I like them to be.
I will be posting more regarding this procedure and the correct way it should be handled > your remark about both the inc(emo1) thingie and in particular the resources eating thingie draws my attention. The first makes sense, didn't think about it that way however.
I will post another question about the resource eating thingie cause I'd like to comprehend that as much as possible > I wouldn't doubt your words ofcourse but when I'd state that that remark also made sense to me I'd have to lie.

Tnx for responding!

Regards Peter
0
 
LVL 17

Expert Comment

by:geobul
ID: 13868799
Hi Peter,

Never fully trust us (and especially me :) because we also make mistakes sometimes.

About eating resources: I said that based on the code you posted only. There could be other parts of the code responsible for that 'effect' also. What I see (and don't like much) is that on every button click a new item is being added to a, most ptobably, listbox. So, the memory will increase until there won't be space left in the listbox.Items property (the listbox has its limitations) and an exception will be raised. I don't find anything else in the procedure that can increase used memory.

Regards, Geo
0
 

Author Comment

by:PeterdeB
ID: 13869845
Hi Geobul!

Thanks for your feedback > I thought the procedure I posted could already be considered as my Cookie Monster :) based on your expertise one would think twice before entering a debate about what can and cannot be the issue in Delphi. So I guess I take the words of experts like you for granted and so far that never appeared a wrong move so consider that as a compliment!

Once again tnx a lot for the feedback, it goes without saying that dudes like me often come to depend on that and it makes this board the best the greatest the number 1 of all places one could visit in order to learn!!
Definetely!

Regards,

Peter :)
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, 7 hours left to enroll

830 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