Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Prism WPF Timer (https://www.delphipraxis.net/136594-wpf-timer.html)

Highttower 3. Jul 2009 13:03


WPF Timer
 
Hallo zusammen,

ich hab ein kleines Problem mit einem Timer in der WPF der C# code dazu sieht so aus:
Zitat:

Zitat von http://www.albahari.com/threading/part3.aspx#_Timers
Code:
using System;

using System.Threading;

 

class Program {

  static void Main() {

    Timer tmr = new Timer (Tick, "tick...", 5000, 1000);

    Console.ReadLine();

    tmr.Dispose();        // End the timer

  }

 

  static void Tick (object data) {

    // This runs on a pooled thread

    Console.WriteLine (data);         // Writes "tick..."

  }

}

Mein Delphi Prism code so:

Delphi-Quellcode:

....

TMyClass = class

....
private
...
t : System.Threading.Timer;
method Tick(data : Object);
...

end;
....
implementation

constructor TMyClass;
begin
    t := new System.Threading.Timer(Tick(new Object), new Object, 5000, 1000);
end;

method TMyClass.Tick(data : Object);
begin
    MessageBox.Show('Hello World');
end;
...

Das ganze gibt dann einen Compile-Error weil angeblich der erste Parameter, nicht passt:
Zitat:

Zitat von VS Console
Warnung 2 (PH2) Beste Übereinstimmung "System.Threading.Timer.<Constructor>(callback : System.Threading.TimerCallback; state: System.Object; dueTime: System.Int32; period: System.Int32)" passt nicht für Parameter 1, Parameter ist "<unknown type>" sollte "System.Threading.TimerCallback" sein P:\TobisSpieleEcke\Pong\Window1.xaml.pas 79 16 Pong

Hat jemand ne Idee woran das hängen könnte?

Vielen Dank,

Tobi

jfheins 3. Jul 2009 13:19

Re: WPF Timer
 
der erste parameter passt nicht :stupid:

Du schreibt da:
Zitat:

t := new System.Threading.Timer(Tick(new Object), new Object, 5000, 1000);
muks zusammen - so wie das da steht würde die Funktion aufgerufen und - in Ermangelung eines Rückgabewert - würde für den Konstruktor nocht mehr im 1. Parameter stehen.

Ich vermute mal du meinst das so:
Zitat:

t := new System.Threading.Timer(Tick, new Object, 5000, 1000);

Highttower 3. Jul 2009 13:22

Re: WPF Timer
 
ja eigentlich meine ich das so, aber wenn ich die Parameter weglasse bekomme ich nen Build Error das er die Funktion mit den Parametern nicht finden kann :(

jfheins 3. Jul 2009 13:26

Re: WPF Timer
 
Dann deklariere die Funktion mal als Klassenfunktion (entspr. static) und nicht als Methode ...

Highttower 3. Jul 2009 13:48

Re: WPF Timer
 
Delphi-Quellcode:

....

TMyClass = class

....
private
...
t : System.Threading.Timer;
class procedure Tick(data : Object);
...

end;
....
implementation

constructor TMyClass;
begin
    t := new System.Threading.Timer(Tick(new Object), new Object, 5000, 1000);
end;

class procedure TMyClass.Tick(data : Object);
begin
    MessageBox.Show('Hello World');
end;
...
Hmm tut immer noch nicht :(

jfheins 3. Jul 2009 14:03

Re: WPF Timer
 
Das meinte ich eigentlich so:

Delphi-Quellcode:
constructor TMyClass;
begin
    t := new System.Threading.Timer(Tick, "", 5000, 1000);
end;

class procedure TMyClass.Tick(data : Object);
begin
    MessageBox.Show('Hello World');
end;
Sonst probiers mal so:
Delphi-Quellcode:
constructor TMyClass;
begin
    timerDelegate : TimerCallback = new TimerCallback({self.}Tick);
 
    t := new System.Threading.Timer(timerDelegate, "", 5000, 1000);
end;

class procedure TMyClass.Tick(data : Object);
begin
    MessageBox.Show('Hello World');
end;
Ich kenne mich mit Prism auch nicht so gut aus :???:

Highttower 3. Jul 2009 14:35

Re: WPF Timer
 
Der Constructor für TimerCallback(<methode>) existiert nicht -.- ... ich bin jetzt auf nen Thread über gegangen und such grad ne art "synchronize" wie ich das aus Java kenne ...

Danke fürs Helfen,

Tobi

Christian S. 3. Jul 2009 16:59

Re: WPF Timer
 
Hallo!

Wenn Du eine Methode als Parameter übergeben möchtest, musst Du ein "@" davor schreiben. Folgender Quelltext zeigt auch die Verwendung von Dispatcher.Invoke, was wohl dem Synchronize aus Java entsprechen dürfte (behaupte jetzt einfach mal, ohne es zu kennen :D)

Delphi-Quellcode:
type
  Window1 = public partial class(System.Windows.Window)
  private
  public
    constructor;
    method Window1_Loaded(sender : Object; e : RoutedEventArgs);
    method Callback(state : Object);
  end;
 
implementation

constructor Window1;
begin
  InitializeComponent();
 
end;
 
method Window1.Window1_Loaded(sender : Object; e : RoutedEventArgs);
begin
  var timer := new System.Threading.Timer(@Callback, nil, 0, 1000);
end;

method Window1.Callback(state : Object);
begin
  Dispatcher.Invoke(DispatcherPriority.Normal, method; begin
    textBox1.Text := DateTime.Now.ToString;
  end);
end;
Grüße
Christian

Highttower 3. Jul 2009 17:02

Re: WPF Timer
 
Ahhhh ... Funktionspoint ...

Vielen lieben Dank

Tobi


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:58 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz