Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Frage zu Thread.Sleep()

Empfohlene Antworten

Veröffentlicht

hallo,

ich hab ein form in dem ich ein Label hab.

in dem label soll alle drei sekunden eine Zufallszahl erscheinen, tut es aber nicht. Wo isn da der Fehler? Oder versteh ich die Funktion von Tread Sleep falsch?

Code


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Threading;

using System.Windows.Forms;


namespace Refresh_Text

{

    public partial class Form1 : Form

    {

        private int Zufallszahl()

        {

            int los;

            Random Zahl = new Random();

            los = Zahl.Next(1, 20);

            return los;

        }


        public Form1()

        {

            InitializeComponent();

            label1.Text = "Irgend";

            for (int i = 0; i < 5; i++)

            {

                label1.Text = Convert.ToString(Zufallszahl());

                Thread.Sleep(3000);

            }

        }


    }

}

Hey,

versuchs mal mit einem Timer.

Da kannste einen Intervall eintragen das er alle x Sec eine ausgewählte funktion aufruft.


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Threading;

using System.Timers;

using System.Windows.Forms;


namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        public void TimerStart(Double Interval)

        {

            System.Timers.Timer aTimer = new System.Timers.Timer();

            aTimer.Elapsed += new ElapsedEventHandler(Zufallszahl);

            aTimer.Interval = Interval;

            aTimer.Enabled = true;

        }



        private void Zufallszahl(object sender, EventArgs e)

        {

            int los;

            Random Zahl = new Random();

            los = Zahl.Next(1, 20);


            label1.Text = Convert.ToString(los);

        }


        public Form1()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            TimerStart(3000);

        }

    }

}

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.