Uhr die rückwärts zählt
-
Irgendwas stimmt noch nicht bei der Minuten anzeige. Wenn zum beispiel die 7.Minuten vorüber ist wird nicht auf sechs geschaltet. Erst nach 30 Sekunden wird sie auf sechs geschaltet.
-
kann ich nicht nachvollziehen...
hast du vielleicht irgendein round() in den code reingemacht?
-
Nein,
hier mal mein Code, habe zur Anzeige ein Formular benutzt:
Code des Threads.cpp//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit2.h" #include "Unit1.h" #include <iostream> #include <ctime> #include <windows.h> #define usleep(n) Sleep((n)/1000) #pragma package(smart_init) //--------------------------------------------------------------------------- // Wichtig: Methoden und Eigenschaften von Objekten der VCL können nur // in Methoden verwendet werden, die Synchronize aufrufen, z.B.: // // Synchronize(UpdateCaption); // // wobei UpdateCaption so aussehen könnte: // // void __fastcall timer::UpdateCaption() // { // Form1->Caption = "In Thread aktualisiert"; // } //--------------------------------------------------------------------------- __fastcall timer::timer(bool CreateSuspended) : TThread(CreateSuspended) { } //--------------------------------------------------------------------------- void __fastcall timer::Execute() { //---- Hier den Thread-Code plazieren---- String Text; unsigned long t = time(0) + 12*60, x; do { x = t - time(0); //cout << "\r" << (x/60) << ":" << (x%60); Text = IntToStr(x/65) + ":" + IntToStr(x%60); Form1->Edit1->Text= Text; //Form1->Edit1->Refresh(); usleep(100000); } while (x >= 0); } //---------------------------------------------------------------------------Code des Threads.h
//--------------------------------------------------------------------------- #ifndef Unit2H #define Unit2H //--------------------------------------------------------------------------- #include <Classes.hpp> //--------------------------------------------------------------------------- class timer : public TThread { private: protected: void __fastcall Execute(); public: __fastcall timer(bool CreateSuspended); }; //--------------------------------------------------------------------------- #endifCode des Formulars:
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include "Unit2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { timer *SecondProcess = new timer(true); SecondProcess->Priority = tpLower; // niedrigere Priorität setzen SecondProcess->Resume(); // Thread jetzt ausführen } //---------------------------------------------------------------------------
-
omfg, threads fuer sowas? borland hat doch timer!
ich setz mal den rotstift an... (geht nur in code tags)//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit2.h" #include "Unit1.h" #include <iostream> #include <ctime> #include <windows.h> #define usleep(n) Sleep((n)/1000) #pragma package(smart_init) //--------------------------------------------------------------------------- // Wichtig: Methoden und Eigenschaften von Objekten der VCL können nur // in Methoden verwendet werden, die Synchronize aufrufen, z.B.: // // Synchronize(UpdateCaption); // // wobei UpdateCaption so aussehen könnte: // // void __fastcall timer::UpdateCaption() // { // Form1->Caption = "In Thread aktualisiert"; // } //--------------------------------------------------------------------------- __fastcall timer::timer(bool CreateSuspended) : TThread(CreateSuspended) { } //--------------------------------------------------------------------------- void __fastcall timer::Execute() { //---- Hier den Thread-Code plazieren---- String Text; unsigned long t = time(0) + 12*60, x; do { x = t - time(0); //cout << "\r" << (x/60) << ":" << (x%60); Text = IntToStr(x/[b]60[/b]) + ":" + IntToStr(x%60); Form1->Edit1->Text= Text; //Form1->Edit1->Refresh(); usleep(100000); } while (x >= 0); } //---------------------------------------------------------------------------ich wuerde mich freuen, wenn du meinen aktuellen code benutzen wuerdest. dann haettest du auch bruchsekunden und eine wesentlich genauere zeitmessung.
-
Text = IntToStr(x/65) + ":" + IntToStr(x%60);65 macht hier keinen Sinn
Kzrt
-
Danke schon mal für alles.
Habe ein neues Problem. Möchte einen Pause Button einfügen. Habe deshalb eine bool Variable "laeuft" in der Unit1.cpp global deklariert. Wenn ich diese aber in der while Schleife Abfrage wird nicht in die Abfrage gesprungen, totz dass diese auf true gesetzt ist. Hier der Code der Abfrage:String Text; unsigned long t = time(0) + 12*60, x; do { if (laeuft == true) { x = t - time(0); //cout << "\r" << (x/60) << ":" << (x%60); Text = IntToStr(x/60) + ":" + IntToStr(x%60); Form1->Edit1->Text= Text; //Form1->Edit1->Refresh(); usleep(100000); } } while (x >= 0);
-
Natürlich. Die zeit läuft ja weiter.
Es bleibt dir nichts anderes übrig als t neu zu setzen wenn lauft auf true geht.
-
Habe das mal so versucht:
if (laeuft == true) { laeuft = false; speicher = t; Form1->Button2->Caption = "Weiter"; } else { laeuft = true; t = speicher; Form1->Button2->Caption = "Pause"; }Funktioniert aber nicht. Die Zeit hält zwar an bei der Anzeige jedoch wenn ich auf weiter drücke kommt die Zeit als hätte ich überhaupt nicht auf Pause gedrückt. Woran kann das jetzt liegen
-
Hier mal der komplette Code:
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include "Unit2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" #include <iostream> #include <ctime> #include <windows.h> #define usleep(n) Sleep((n)/1000) #pragma package(smart_init) unsigned long t, speicher; bool laeuft; TForm1 *Form1; class timer : public TThread { private: protected: void __fastcall Execute(); public: __fastcall timer(bool CreateSuspended); }; //--------------------------------------------------------------------------- // Wichtig: Methoden und Eigenschaften von Objekten der VCL können nur // in Methoden verwendet werden, die Synchronize aufrufen, z.B.: // // Synchronize(UpdateCaption); // // wobei UpdateCaption so aussehen könnte: // // void __fastcall timer::UpdateCaption() // { // Form1->Caption = "In Thread aktualisiert"; // } //--------------------------------------------------------------------------- __fastcall timer::timer(bool CreateSuspended) : TThread(CreateSuspended) { } //--------------------------------------------------------------------------- void __fastcall timer::Execute() { //---- Hier den Thread-Code plazieren---- String Text; unsigned long x, y; t = time(0) + 1*60; do { if (laeuft == true) { x = t - time(0); //cout << "\r" << (x/60) << ":" << (x%60); Text = IntToStr(x/60) + ":" + IntToStr(x%60); Form1->Edit1->Text= Text; //Form1->Edit1->Refresh(); usleep(100000); } } while (x >= 0); //---- Hier den Thread-Code plazieren---- } timer *SecondProcess = new timer(true); //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { SecondProcess->Priority = tpLower; // niedrigere Priorität setzen SecondProcess->Resume(); // Thread jetzt ausführen laeuft = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { if (laeuft == true) { laeuft = false; speicher = t; Form1->Button2->Caption = "Weiter"; } else { laeuft = true; t = speicher; Form1->Button2->Caption = "Pause"; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { SecondProcess->Terminate(); } //---------------------------------------------------------------------------
-
Würde es so versuchen.
void __fastcall timer::Execute() { String Text; unsigned long x, y; t = time(0) + 1*60; do { if (laeuft == true) { x = t - time(0); Text = IntToStr(x/60) + ":" + IntToStr(x%60); Form1->Edit1->Text= Text; } usleep(100000); // muss raus aus dem if sonst haben wir hohe last } while (x >= 0); //---- Hier den Thread-Code plazieren---- } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { if (laeuft == true) { laeuft = false; speicher = t - x; Form1->Button2->Caption = "Weiter"; } else { laeuft = true; t = speicher; Form1->Button2->Caption = "Pause"; } }Keine Garantie. Kanns nicht ausprobieren mein compiler versteht so komische sachen wie __fastcall und TObject nicht

Kurt
-
Das funktioniert auch nicht!
Habe auch mal versuch x und t getrennt abzuspeichern und wieder zu setzten, funktioniert aber auch nicht.
-
Hallo
Nimm doch einfach TTimer (wie schon mal gesagt), mit einem Intervall von 1000ms kannst du nen einfachen Countdown machen, den du auch mit der Eigenschaft Enabled unterbechen kannst.
bis bald
akari
-
Ja das war Blödsinn.
x ist die zeit die noch übrig ist.
Wenn t von timer und TForm1 das gleiche ist ( müsste wahrscheinlich global sein ) dann sollte es so funktionieren. x muss dann natürlich auch global gemacht werden.void __fastcall TForm1::Button2Click(TObject *Sender) { if (laeuft == true) { laeuft = false; speicher = x; Form1->Button2->Caption = "Weiter"; } else { laeuft = true; t = speicher + time(0); Form1->Button2->Caption = "Pause"; } }edit: akaris lösung wäre natürlich nicht nur einfacher sondern auch wesentlich eleganter. ( keine globals notwendig )
edit2: speicher = x + time(0);
edit3: t = speicher + time(0); Ohne zu testen bin ich meist knapp daneben.