Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't this work? 1

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
Hi,

I have a simple thing I want to do:
Move a TImage around in a form.
The TImage holds an icon of a magnifying glass, and I am trying to move the image around in a continuous circle (while another procedure is performing a search).

My problem is that the image does not move. I have tried running the code below in a thread, but still nothing happens.

What am I doing wrong?

Thanks in advance.

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  i:integer;
begin
  for i:= 0 to 10 do
  begin
    Image1.Top  := Image1.Top+3;
    Image1.Left := Image1.Left-3;
    sleep(1000);
    Image1.Top  := Image1.Top+3;
    Image1.Left := Image1.Left+3;
    sleep(1000);
    Image1.Top  := Image1.Top-3;
    Image1.Left := Image1.Left+3;
    sleep(1000);
    Image1.Top  := Image1.Top-3;
    Image1.Left := Image1.Left-3;
    sleep(1000);
  end;
end;




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
Your are right it doesnt run.
A few points worth mentioning
You don't give any details about how this is implemented in a thread.
The Sleep() function is generally a bad idea, it does what it says and puts the cpu to sleep for however long you specify I wouldn't recommend it even in normal code.
The code I have posted runs as a normal (non threaded) procedure and uses my own delay library function.
Note the Application.ProcessMessages call.
I havn't tested this as a threaded call it might not work!



Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  i:integer;
begin
  for i:= 0 to 10 do
  begin
    application.ProcessMessages;
    Image1.Top  := Image1.Top+3;
    Image1.Left := Image1.Left-3;
    waitms(100);
    Image1.Top  := Image1.Top+3;
    Image1.Left := Image1.Left+3;
    waitms(100);Image1.Top  := Image1.Top-3;
    Image1.Left := Image1.Left+3;
    waitms(100);
     Image1.Top  := Image1.Top-3;
    Image1.Left := Image1.Left-3;
    waitms(100);
  end;
end;
waitms looks like this

Code:
procedure WaitMs(T: DWORD);
var  tc :DWORD;
begin
   tc := GetTickCount;
   repeat
        Application.ProcessMessages;
   until ((GetTickCount - tc) >= T);
end;

Steve: Delphi a feersum engin indeed.
 
Thanks.




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
The Sleep() function is generally a bad idea, it does what it says and puts the cpu to sleep for however long you specify I wouldn't recommend it even in normal code."

This is what my Delphi helpfile says:

"Delay program execution for a specified number of microseconds."

Seems different and I can see what the problem is if it would freeze the CPU for X ms but it doesn't, it just pauses the execution of the thread it is in for X ms.


[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
Great Delphi Websites faq102-5352
 
Bobbatfet The helpfile is being er, economical with the truth on this point. There are several other prefered ways of creating a delay, this is well documented.

I honestly dont know the effect of sleep() in multithreaded application, but even if it just stops one thread dead, this isnt good pratice in an event driven language IMHO.

Yes I have used sleep() myself, sometimes it dosnt matter if things just stop, but in the case of an animation?


Steve: Delphi a feersum engin indeed.
 
Hi,

in fact Sleep() is sometimes a necessity in multithreaded apps, it is correct that sleep stops the current thread, this is sometimes a must to give other threads time to do some processing.
using sleep in a single threaded app (the vcl main thread) is generally a bad idea, use sggaunt's routine to create delays...

full info can be found here :


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Ok but the way I usually use the Sleep() function is to give a app time to minimize before continuing execution.
(Most apps I make are for games to join, for example, you get a message from someone with an IP address and port to join a server, my progam allows you enter the ip and port then and there before joining the server without having to either write down an ip:port and launches the game so that itll automatically joins the server when the game starts.)

So my code looks something like this:

procedure JoinGame;
begin
{... code to create parameters for game to
join server with user specified settings ...}
Application.Minimize;
Sleep(100);
ShellExecute(blablabla);
end;

Is there anything wrong/bad programming in this?


[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
Great Delphi Websites faq102-5352
 
In a utility I am writing, I carry out the following steps
1) I enumerate windows
2) Bring to the front and send keystrokes to windows matching given criteria
3) Resize and repositition said windows.

I have observed that my utility actually makes a mess, with keystrokes being sent/processed erratically and resizing sometimes working haphazardly. That is, unless I introduce a number of sleep(250) statements after each interaction with the other applications.

Any idea what could be the cause. I am willing to forward more info and code snippets if necessary.

I liked ssgaunt's routine and will use it instead of sleep().

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top