Solved

Pb with multimedia timer in an object

Posted on 1997-06-14
4
261 Views
Last Modified: 2010-04-04
What is wrong with this source code ?
I want to use a multimedia timer in an object (TCanal).

At the first event, instead of calling one time the function, TCanal.Lecture is called two time by the timer and cause access violation error : access violation at 0xa8fee0 read of adress 0xffffffff


//*******************************************************************************************
//TCANAL OBJECT
//*******************************************************************************************

TCanal = class(TPanel)
  private
    timer : MMRESULT;
    procedure Lecture(uID : UINT; uMsg : UINT; dwUser : DWORD; dw1 :
DWORD; dw2 : DWORD); pascal;
    procedure StartMesure;
    procedure StopMesure;
  public
  constructor Create(Owner: TComponent); override;
  end;

constructor TCanal.Create(Owner: TComponent);
begin
  inherited Create(Owner);              { Initialisation des parties héritées }
  Parent := TWinControl(Owner);
  StartMesure;
end;

//*******************************************************************************************
// Multimedia timer
//*******************************************************************************************


procedure TCanal.Lecture(uID : UINT; uMsg : UINT; dwUser : DWORD; dw1 :
DWORD; dw2 : DWORD);
begin
     beep;
end;

procedure TCanal.StartMesure;
begin
     timeBeginPeriod(1);
     timer := timeSetEvent(100, 0, @TCanal.Lecture, 0, TIME_PERIODIC );
end;

procedure TCanal.StopMesure;
begin
     timeKillEvent(timer);
     timeEndPeriod(1);
end;

//******************************************************************************************

Thanks in advance.

0
Comment
Question by:turnar
  • 2
4 Comments
 
LVL 1

Expert Comment

by:sleach
ID: 1337029
I can find nothing wrong with Canal.Lecture.  However, you should place the suspect code inside a try/except block and see if that fixes the problem.  
example:

procedure TCanal.Lecture(uID : UINT; uMsg : UINT; dwUser : DWORD; dw1 :
DWORD; dw2 : DWORD);
begin
try
beep;
except
{Do nothing}
end;
end;
0
 

Author Comment

by:turnar
ID: 1337030
It cause access violation error, even with the try/except block.
I get this answer in comp.lang.pascal.delphi.misc :

>>>>>>>>>>>>>>>>>>>>>

Lecture() cannot be an object method, for starters. The timer
procedure is used by the Win32 API, which knows nothing about
the Self-pointer passed to all methods.

Secondly, being a Win32 API, you have to use the 'stdcall' calling
convention instead of 'pascal'.

Thirdly (and least seriously), there is a problem with the
definitions in Windows.pas:

type
  TFNTimeCallBack = procedure(uTimerID, uMessage: UINT; dwUser, dw1,
dw2: DWORD) stdcall;

function timeSetEvent(uDelay, uResolution: UINT;
                      lpFunction: TFNTimeCallBack;
                      dwUser: DWORD; uFlags: UINT): MMRESULT stdcall;

(Basically, Delphi forgets the 'stdcall' in TFNTimeCallback. This
isn't fatal because you can work around it using '@')

>>>>>>>>>>>>>>>>>>>

So, how can I do it ? I need a multimedia timer every time a TCanal instance is created.
Thanks in advance.

Christophe.
0
 
LVL 1

Expert Comment

by:sleach
ID: 1337031
I thought that a try/except block might do the trick.  Im not sure what else might help, so grade me an F, and let others have a try.  Wish I could be of more help. ;)
0
 
LVL 3

Accepted Solution

by:
sperling earned 100 total points
ID: 1337032
Go to my homepage, http://www.info-pro.no/sperling and then download my TimerPool units (with source ofcourse). These implement a THiResTimer class, using a single multimedia timer to distribute timer events to multiple THiResTimer instances. The code should get you started if you won't use it directly.


Regards,

Erik.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

19 Experts available now in Live!

Get 1:1 Help Now