Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Pb with multimedia timer in an object

Posted on 1997-06-14
4
276 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

790 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