Board index » cppbuilder » TTimer OnTimer

TTimer OnTimer

I have problems assigning a ontimer event, it's probably something simple.
Could someone tell me where the problem is:

--//--
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
  class WaitForTimerClass
  {
    private:
      bool Done;
    public:
      bool __fastcall WaitForTimerClass::GetDone(void)
      {
        return Done;
      }
      void __fastcall WaitForTimerClass::SetDone(bool Finished)
      {
        Done = Finished;
      }
  };

  try
  {
    Application->Initialize();
    Application->Title = "Belgacom";
    TSplashForm1 *SplashForm = new TSplashForm1(NULL);    //Create the
SplashForm
    SplashForm->Show();      //  Show and update
    SplashForm->Update();

    WaitForTimerClass TimerWait;
    TimerWait.SetDone(false);
    TTimer *Timer = new TTimer(NULL);
    Timer->Interval = 5000;
    Timer->OnTimer = TimerWait.SetDone(true);
    Timer->Enabled = true;
    while (!TimerWait.GetDone())
    {
      Application->ProcessMessages();
    }
    delete Timer;

    SplashForm->Close();      //Close and Delete SplashForm
    delete SplashForm;
    Application->CreateForm(__classid(TMainForm1), &MainForm1);
    Application->Run();
  }
  catch (Exception &exception)
  {
    Application->ShowException(&exception);
  }
  return 0;

Quote
}

 

Re:TTimer OnTimer


Quote
"Blankspace Steven" <Blackfox...@yahoo.com> wrote in message

news:3b095b6f_2@dnews...

Quote
> I have problems assigning a ontimer event, it's probably something simple.
> Could someone tell me where the problem is:

OnTimer is a TNotifyEvent.  This means it only takes a pointer to a
__fastcall method taking TObject* as a single parameter.  You need to make
your SetDone() method look like this:

    void __fastcall SetDone(TObject* Sender)
    { ... }

I would also like to point out that you should consider putting all of your
code between ...

    Application->Title = "Belgacom";
... and ...
    Application->CreateForm(__classid(TMainForm1), &MainForm1);

... in a different unit or function above WinMain.  The IDE has a habit of
tromping on unsuspecting code in WinMain().  This way, if the IDE destroys
the function call you will only need to add the function call back in.  It's
a little harder (and annoying) to recreate code.

Been burned a few times,

- Clayton

Re:TTimer OnTimer


I changed the code to

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
  class WaitForTimerClass
  {
    private:
      bool Done;
    public:
      bool __fastcall WaitForTimerClass::GetDone(void)
      {
        return Done;
      }
      void __fastcall WaitForTimerClass::SetDone(bool Finished)
      {
        Done = Finished;
      }
      void __fastcall OnTime(TObject* Sender)
      {
        Done = true;
      }

  };

  try
  {
    Application->Initialize();
    Application->Title = "Scratchpad";
    TSplashForm1 *SplashForm = new TSplashForm1(NULL);    //Create a
SplashForm
    SplashForm->Show();      //  Show and update
    SplashForm->Update();

    WaitForTimerClass TimerWait;
    TimerWait.SetDone(false);
    TTimer *Timer = new TTimer(NULL);
    Timer->Interval = 5000;
    Timer->OnTimer = TimerWait.OnTime(NULL);
    Timer->Enabled = true;
    while (!TimerWait.GetDone())
    {
      Application->ProcessMessages();
    }
    delete Timer;

    SplashForm->Close();      //Close and Delete SplashForm
    delete SplashForm;
    Application->CreateForm(__classid(TMainForm1), &MainForm1);
     Application->Run();
  }
  catch (Exception &exception)
  {
    Application->ShowException(&exception);
  }
  return 0;

Quote
}

But is still gives me an error: not an allowed type
"Clayton M. Arends" <nospam.claytonare...@hotmail.com> wrote in message
news:3b0976ae$1_1@dnews...
Quote
> "Blankspace Steven" <Blackfox...@yahoo.com> wrote in message
> news:3b095b6f_2@dnews...

> > I have problems assigning a ontimer event, it's probably something
simple.
> > Could someone tell me where the problem is:

> OnTimer is a TNotifyEvent.  This means it only takes a pointer to a
> __fastcall method taking TObject* as a single parameter.  You need to make
> your SetDone() method look like this:

>     void __fastcall SetDone(TObject* Sender)
>     { ... }

> I would also like to point out that you should consider putting all of
your
> code between ...

>     Application->Title = "Belgacom";
> ... and ...
>     Application->CreateForm(__classid(TMainForm1), &MainForm1);

> ... in a different unit or function above WinMain.  The IDE has a habit of
> tromping on unsuspecting code in WinMain().  This way, if the IDE destroys
> the function call you will only need to add the function call back in.
It's
> a little harder (and annoying) to recreate code.

> Been burned a few times,

> - Clayton

Re:TTimer OnTimer


Quote
"Blankspace Steven" <Blackfox...@yahoo.com> wrote in message

news:3b09798b_2@dnews...

Quote
> I changed the code to ...
> ... But is still gives me an error: not an allowed type

Replace:

    Timer->OnTimer = TimerWait.OnTime(NULL);

... with ...

    Timer->OnTimer = &TimerWait.OnTime;

HTH,

- Clayton

Re:TTimer OnTimer


I'm curious, did you NOT ever see the code I gave you when you asked this
exact same question last week?

Gambit

Quote
"Blankspace Steven" <Blackfox...@yahoo.com> wrote in message

news:3b09798b_2@dnews...
Quote
> I changed the code to

<snip>

Re:TTimer OnTimer


Yes, I saw it but I wanted to allocate and free the resources on the same
spot.
Quote
"Remy Lebeau" <gambi...@gte.net> wrote in message news:3b09c805$1_1@dnews...
> I'm curious, did you NOT ever see the code I gave you when you asked this
> exact same question last week?

> Gambit

> "Blankspace Steven" <Blackfox...@yahoo.com> wrote in message
> news:3b09798b_2@dnews...
> > I changed the code to
> <snip>

Re:TTimer OnTimer


To what purpose, though?  It's a lot simplier to just drop the TTimer onto
your splash form so the splash form can close itself rather than trying to
get working the method you're trying.

Gambit

Quote
Blankspace Steven <Blackfox...@yahoo.com> wrote in message

news:3b09fa71_2@dnews...
Quote
> Yes, I saw it but I wanted to allocate and free the resources on the same
> spot.

Re:TTimer OnTimer


I changed the code to what you sent me. Do you have an example on how you
write an event handler for a component that you make dynamically. (just
curious)

Regards,
Steven

Quote
"Remy Lebeau" <gambi...@gte.net> wrote in message news:3b0a92bd$2_1@dnews...
> To what purpose, though?  It's a lot simplier to just drop the TTimer onto
> your splash form so the splash form can close itself rather than trying to
> get working the method you're trying.

> Gambit

> Blankspace Steven <Blackfox...@yahoo.com> wrote in message
> news:3b09fa71_2@dnews...
> > Yes, I saw it but I wanted to allocate and free the resources on the
same
> > spot.

Re:TTimer OnTimer


Since we're discussing TTimer, I'll give you a TTimer example:

TTimer *timer = NULL;

__fastcall TMyForm::TMyForm(TComponent *Owner)
    : TForm(Owner)
{
    timer = new TTimer(this);
    timer->Enabled = false;
    timer->Interval = 5000;
    timer->OnTimer = MyTimer;
    timer->Enabled = true;

Quote
}

void __fastcall TMyForm::MyTimer(TObject *Sender)
{
    timer->OnTimer = NULL;
    timer->Enabled = false;
    Close();

Quote
}

Gambit

Quote
"Blankspace Steven" <Blackfox...@yahoo.com> wrote in message

news:3b0aa464$1_2@dnews...
Quote
> I changed the code to what you sent me. Do you have an example on how you
> write an event handler for a component that you make dynamically. (just
> curious)

Other Threads