0

This is my requirement I have to use while loop inside timer, after launch application after click on button UI is locked not able to move and text is not diplaying at textbox too

below is the code

using System;
using System.Windows.Forms;
namespace WinScreenLocked
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int Number = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            while (true)
            {
                textBox1.Text = Number.ToString();
                Number++;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }
    }
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Rajanikant Hawaldar
  • 314
  • 1
  • 5
  • 12
  • 2
    What is your question? You are locking the UI in the thread of the UI (main thread). You need to create a different thread, which handles the change of the controls. If you change the controls in a different thread you need to invoke the action if invoke is required. Here is an example if you dont want to lock the UI. https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls – Sebastian Siemens May 06 '19 at 06:34
  • Looks like code in the post satisfies the requirement - there is infinite loop that freezes UI that starts on timer (assuming you've configured timer correctly)... The requirement is kind of strange (it is rare to hear request to make UI non-responsive)... but since code matches the requirement it is unclear what exactly you have problem with? – Alexei Levenkov May 06 '19 at 06:35
  • This is a sample project created to post, In actual project there is while loop inside timer tick event, while loop will loops untill it gets value from one api so in that period only UI is locking – Rajanikant Hawaldar May 06 '19 at 06:39

3 Answers3

1
// Create a 30 min timer 
timer = new System.Timers.Timer(1800000);

// Hook up the Elapsed event for the timer.
timer.Elapsed += OnTimedEvent;

timer.Enabled = true;


private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    // do stuff
}

with the usual caveats of: timer won't be hugely accurate and might need to GC.KeepAlive(timer)

See also: Why does a System.Timers.Timer survive GC but not System.Threading.Timer?

janavarro
  • 869
  • 5
  • 24
  • subscribed , forgot to add here sorry – Rajanikant Hawaldar May 06 '19 at 06:50
  • Then it might be because you need an async timer, check this answer. https://stackoverflow.com/questions/15879476/creating-a-background-timer-to-run-asynchronously If you rely in a API it is a must, your program will be frozen until it recieves a response from the API – janavarro May 06 '19 at 06:54
1

you can stop the thread to block the ui i.e using

    System.Threading.Thread.Sleep(2000);

it takes miliseconds in above 2000 miliseconds is equal to 2 seconds.

Usman Khalid
  • 140
  • 1
  • 8
0

Seeing as this is winforms you can use Application.DoEvents() to process the UI refresh.

See this : https://social.msdn.microsoft.com/Forums/en-US/b1b1969d-8a51-496c-9274-a0ac1708a8b4/what-does-applicationdoevents-actually-do?forum=csharplanguage

RandomCoder
  • 134
  • 9