Autor Beitrag
snowy1980
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Di 28.04.09 08:38 
Hallo,

Ich möchte eine while-Schleife alle 10 ms durchlaufen lassen. Was muss ich an meinem Quellcode ändern?

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
// Timer anlegen mit Intervall 10ms
            Timer t = new Timer(); 
            t.Interval = 10

            do
            {
                //Timer starten
                t.Start();

               //Funktionen
                
            }
            while (t);


Danke!


Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Di 28.04.2009 um 11:33
bakachan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 503
Erhaltene Danke: 34

W7 (x64) Ultimate
C# / VB.NET (VS2010 Ultimate)
BeitragVerfasst: Di 28.04.09 08:50 
Wenn du deine Schleife vom Timer gesteuert ausführen willst muss die an das .Tick Event des Timers.

also
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
private void BlaBla()
{
    Timer timer = new Timer();
    timer.Interval = 10;
    timer.Tick += new EventHandler(Timer_Tick);
}

private void Timer_Tick(object sender, EventArgs e)
{
    //do something
}
snowy1980 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Di 28.04.09 13:11 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
public void TimerReqAll(object sender, EventArgs e)
        {
            Timer timer = new Timer();
            timer.Interval = 500;
            timer.Tick += new EventHandler(Timer_Tick);

            do
            {
                timer.Start();

            }
            while (true);
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            MessageBox.Show("Test");
        }


So hängt sich die Anwendung auf. Dachte jetzt erscheint alle 0,5 sec die Messagebox..?

Ist es auch möglich, während der Timer läuft, auf dem Formular Button wo diese Aktion gestartet wird, Stop anzuzeigen?
Damit die Aktion unterbrochen werden kann?
snowy1980 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Di 28.04.09 13:15 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
public void TimerReqAll(object sender, EventArgs e)
        {
            Timer timer = new Timer();
            timer.Interval = 500;
            timer.Tick += new EventHandler(Timer_Tick);
            timer.Start();
            
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            do
            {
                MessageBox.Show("Test");

            }
            while (true);
            
        }


so klappt es jetzt mit der Textbox anzeigen.

Aber wie kann ich das über den gleichen Button beenden?
bakachan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 503
Erhaltene Danke: 34

W7 (x64) Ultimate
C# / VB.NET (VS2010 Ultimate)
BeitragVerfasst: Di 28.04.09 13:23 
Zum beenden
Timer.Stop()
die logik wann was gemacht wird bleibt dir überlassen
snowy1980 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Di 28.04.09 14:11 
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
enum CalcState
        {
            Pending,
            Calculating,
            Canceled,
        }

        CalcState _state = CalcState.Pending;

       public void TimerReqAll(object sender, EventArgs e)
        {
            Timer timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += new EventHandler(Timer_Tick);

            //Button-Logik
            switch (_state)
            {
                // Start 
                case CalcState.Pending:
                    _state = CalcState.Calculating;
                    btnTimerReqAll.Text = "Cancel";

                    timer.Start();
                    break;

                // Cancel 
                case CalcState.Calculating:
                    _state = CalcState.Canceled;
                    btnTimerReqAll.Enabled = false;
                    btnTimerReqAll.Text = "&TimerReqAll";

                    timer.Stop();
                    break;

            }
       }

       private void Timer_Tick(object sender, EventArgs e)
       {
            do
            {
                MessageBox.Show("Test");
            }
            while (true);
       }


so weit klappt die Logik für den Button. Nach klicken steht dann Cancel drauf und es erscheinen Die Messageboxen.
Aber der Fokus ist auf den Boxen und ich kann nicht Cancel drücken..
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4795
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 28.04.09 15:02 
Die MessageBox ist ja auch modal, so daß dein Hauptprogramm blockiert wird.

Verwende lieber eine intensive Rechenroutine als Test, z.B. eine Zählschleife.

Und du solltest keine Endlosschleife im TimerTick verwenden, denn der TimerTick blockiert ebenfalls die GUI.

Wahrscheinlich ist ein BackgroundWorker für deine Aufgabe aber besser geeeignet, s. MSDN.