Recent

Author Topic: Need linear execution  (Read 5597 times)

user5

  • Sr. Member
  • ****
  • Posts: 357
Need linear execution
« on: July 23, 2014, 12:21:28 am »
Greetings. Can anyone tell me how to have my statements execute in a linear way instead of executing all at once? In other words, I need a particular statement to finish completely before going on to the next statement. I’ve been looking at TActionList and multithreading but they don’t seem to be what I need. Thanks.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Need linear execution
« Reply #1 on: July 23, 2014, 12:26:23 am »
AFAIK there is no way to execute anything at once, unless you have more threads and processes. If you wait them to terminate you have what you intend.
« Last Edit: July 23, 2014, 12:29:10 am by typo »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Need linear execution
« Reply #2 on: July 23, 2014, 12:38:20 am »
a typical lazarus application is linear there is no "all at once" mode to it. It is a single thread that unless a command finishes there is no way to execute the next. What exactly is the problem you are facing?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Need linear execution
« Reply #3 on: July 23, 2014, 02:49:12 am »
Well, I solved my problem by using the IdleTimer in between each statement. In one sense you’re correct when you say that each statement is completed one after the other but in practice the events can happen so fast that one statement doesn’t always have a chance to finish before the next one is started and they overlap each other, especially if you’re dealing with graphics. In the first example below, which doesn’t work right, I want to load a graphics file and then perform an analysis of the file (Button2Click). The next example, used with the IdleTimer, works fine because it gives a few microseconds delay before Button2Click is called.

             This doesn't work
procedure TForm1.Button1Click(Sender: TObject);
var picture:TPicture;
begin
 picture := TPicture.create;
 Image1.picture.loadfromfile('pix1.png');
 picture.free;
 Button2Click(Sender);
end;

             These two work
procedure TForm1.Button1Click(Sender: TObject);
var picture:TPicture;
begin
 picture := TPicture.create;
 Image1.picture.loadfromfile('pix1.png');
 picture.free;
 IdleTimer1.interval := 100;
 IdleTimer1.enabled := true;
end;

procedure TForm1.IdleTimer1Timer(Sender: TObject);
begin
 Button2Click(Sender);
 IdleTimer1.enabled := false;
end;

Thanks for your quick responses!

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Need linear execution
« Reply #4 on: July 23, 2014, 02:54:03 am »
that does not make any sense
1) the tpicture create and destroy is not needed so simple remove it.
2) if the only thing needed was a small delay then using sleep(0) should do the job with out the timer staff at all.
3) not knowing what button2click does the only thing I can say with certainty is that your analysis is incomplete
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Need linear execution
« Reply #5 on: July 23, 2014, 03:28:29 am »
Application.ProcessMessages also should help.

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Need linear execution
« Reply #6 on: July 23, 2014, 04:37:27 am »
1. My use of picture := TPicture.Create is just an old habit of mine but it’s not really the issue.
2. I’ve tried using a delay or sleep call between the two statements tens of times and it just doesn’t work here. The picture loads fine but the information returned is always wrong and different each time. Otherwise I wouldn’t have used the IdleTimer or posted these messages.
3. Button2Click simply initiates some code to find the centerline of a waveform. You say that my analysis is incorrect but the bottom line is that your suggestion doesn’t function correctly in this case. I have to go with what works. I can post the actual Lazarus test project if U want. It’s not big. If you run it then you’ll see what I mean. Typo, can you direct me to some simple examples of ProcessMessages?

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Need linear execution
« Reply #7 on: July 23, 2014, 05:08:38 am »
Try this
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  Image1.picture.loadfromfile('pix1.png');
  //Sleep(100); // Try uncommenting this sleep line too if needed
  Application.ProcessMessages;
  Button2Click(Sender);
end;

Your "old habit" with TPicture makes no sense, and there is no reason to have it there.
« Last Edit: July 23, 2014, 05:10:42 am by User137 »

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Need linear execution
« Reply #8 on: July 23, 2014, 08:18:19 am »
User137, I suggest that you go to http://wiki.freepascal.org/Developing_with_Graphics. If you go on down the page, you will find an example with the declaration:

var MyPicture: TPicture;
   begin
     MyPicture := TPicture.Create; etc.

I used picture instead of MyPicture but it's the same thing, so I’m not the only one with this ‘old habit’. That’s the way I learned it and I didn’t know until today that it’s not necessary.

As far as the earlier issue with ‘non-linear’ statements not executing sequentially, I followed typo’s suggestion and thusly hit on a successful combination of short delays and the use of Application.ProcessMessages, without the use of IdleTimer. Thank you, typo, for actually being of some help.


User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Need linear execution
« Reply #9 on: July 24, 2014, 11:38:06 am »
It is absolutely not the same thing. There are code like this in the wiki:
Code: [Select]
var
  MyPicture: TPicture;
begin
  MyPicture := TPicture.Create;
  try
    // Load from disk
    MyPicture.LoadFromFile(MyEdit.Text);
 
    // Here you can use MyPicture.Graphic.Canvas to read/write to/from the image
 
    // Write back to another disk file
    MyPicture.SaveToFile(MyEdit2.Text);
  finally
    MyPicture.Free;
  end;
end;
There it is not just some decoration variable, but it's the one being used for the graphics. The variable you are using is Image1.picture, that is your "MyPicture". You don't need to declare it second time.

 

TinyPortal © 2005-2018