Recent

Author Topic: Problems with sleep  (Read 5935 times)

tehshy

  • Newbie
  • Posts: 3
Problems with sleep
« on: December 31, 2010, 12:02:13 am »
I'm trying to write visualisation of sorting algorithms. As simple as possible for starter.

I did the GUI and canvas stuff. Then I thought it'd work if I simply put 'sleep(200)' and drawing function into each sorting algorithm into right place... The programme freezes for the right amount of time without drawing anything but the last (already sorted) status.

Code: [Select]
procedure TForm1.shBubbleS1(var Tab: array of Integer);
var i,j,x:Integer;
begin
for j:=1 to arSize-1 do
        for i:= 1 to arSize-1 do
                if Tab[i] > Tab[i+1] then
                begin
                x := Tab[i];
                Tab[i] := Tab[i+1];
                Tab[i+1] := x;
                Sleep(100);
                Draw(Tab,i,i+1);
                end;

end;

I guess I'll need to do some research on threads? Simply putting sorting to one thread, drawing to another(which would be stopped by sleep();) would do the job?
« Last Edit: December 31, 2010, 12:03:48 am by tehshy »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Problems with sleep
« Reply #1 on: December 31, 2010, 12:33:01 am »
What is:

Code: [Select]
Draw(Tab,i,i+1);

What does it do?

tehshy

  • Newbie
  • Posts: 3
Re: Problems with sleep
« Reply #2 on: December 31, 2010, 01:03:52 am »
Code: [Select]
procedure TForm1.Draw(tab: Array of Integer; a:Integer; b:Integer);
var i,stX,stY,barW,space:Integer;

begin
if arSize > 200 then space:=2
else space:=1;
barW:=((img.Width-(arSize*space)) div arSize)-1;
stY:=img.Height-10;
stX:= 5;
img.Canvas.Brush.Color:=clWhite;      {cleaning}
img.Canvas.FillRect(0,0,imgPlotno.Width,imgPlotno.Height);

for i:=1 to arSize do
begin
  if (i=a) or (i=b) then
     img.Canvas.Brush.Color:=clGreen
  else
     img.Canvas.Brush.Color:=clBlue;
  img.Canvas.Rectangle(stX+ (i-1)*(barW+Space), stY,stX+ (i-1)*(barW+space)+barW,stY - tab[i]);
end

end;              
img is of TImage type.
I guess there's no need for redrawing each rectangle, but i'm pretty sure too it doesn't matter now.
« Last Edit: December 31, 2010, 01:10:36 am by tehshy »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Problems with sleep
« Reply #3 on: December 31, 2010, 01:12:52 am »
For me it works without freezing.

My code:

Code: [Select]
procedure TForm1.shBubbleS1(var Tab: array of Integer);
var
  i,j,x:Integer;
begin
  for j:=1 to high(tab)-1 do
  for i:= 1 to high(tab)-1 do
    if Tab[i] > Tab[i+1] then
    begin
      x := Tab[i];
      Tab[i] := Tab[i+1];
      Tab[i+1] := x;
      Sleep(100);
      D(Tab,i,i+1);
    end;
end;

procedure TForm1.D(tab: Array of Integer; a:Integer; b:Integer);
var
  i,stX,stY,barW,space:Integer;
begin
  if high(tab)+1 > 200 then space:=2
  else space:=1;
  barW:=((imgPlotno.Width-((high(tab)+1)*space)) div high(tab)+1)-1;
  stY:=imgPlotno.Height-10;
  stX:= 5;
  imgPlotno.Canvas.Brush.Color:=clWhite;      {cleaning}
  imgPlotno.Canvas.FillRect(0,0,imgPlotno.Width,imgPlotno.Height);

  for i:=1 to high(tab)+1 do
  begin
    if (i=a) or (i=b) then
      imgPlotno.Canvas.Brush.Color:=clGreen
  else
     imgPlotno.Canvas.Brush.Color:=clBlue;

  imgPlotno.Canvas.Rectangle(stX+ (i-1)*(barW+Space), stY,stX+ (i-1)*(barW+space)+barW,stY - tab[i]);
end
end;

procedure TForm1.Button21Click(Sender: TObject);
var
  tab :array[0..4] of integer = (1,5,3,4,2);
begin
  shbubbles1(tab);
end;       
« Last Edit: December 31, 2010, 01:18:49 am by typo »

tehshy

  • Newbie
  • Posts: 3
Re: Problems with sleep
« Reply #4 on: December 31, 2010, 06:37:34 pm »
Thank you, i used TImage in my code, which caused the problem. Finally what I needed was Application.ProcessMessages;.

 

TinyPortal © 2005-2018