Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
why does this code freeze my form in visual basic


VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click




    For i As Integer = 1 To 5

        System.Threading.Thread.Sleep(1000)
        TextBox1.Text = "/"
        System.Threading.Thread.Sleep(1000)
        TextBox1.Text = "\"
    Next




End Sub


What I have tried:

searchign the internet but i did not find enything
Posted
Updated 25-Jul-17 5:54am

Because you are telling the UI thread to go to sleep twice inside a loop. And until the event handler exits, the UI thread can do nothing else. Since the UI thread is what changes the screen, and handles all user input, your form locks up until the method exits.
 
Share this answer
 
Comments
dolfijn3000 23-Jul-17 10:07am    
ow ok that explains it but i want it to wait inside the loop for a secodn how do i do that then withoud freezing the form
Dave Kreskowiak 23-Jul-17 11:48am    
Move all the work to a background thread and Invoke a method on the UI form to update the TextBox with your new text.
private void button1_Click(object sender, EventArgs e)
        {
            WorkerMethod();
        }

        protected async void WorkerMethod()
        {
            Task workerTask = Task.Factory.StartNew(() =>
            {
                for(int i = 0; i < 5; i++)
                {
                    SetTextBox(@"\");
                    Thread.Sleep(1000);
                    SetTextBox(@"/");
                    Thread.Sleep(1000);
                }
            });

            await workerTask;

            SetTextBox(string.Empty);
        }

        protected void SetTextBox(string message)
        {
            if (textBox1.InvokeRequired)
            {
                textBox1.Invoke(new Action(() => SetTextBox(message)));
            }
            else
            {
                textBox1.Text = message;
            }
        }
OriginalGriff 23-Jul-17 11:59am    
Dave, get yourself a coffee - he's a VB learner! :laugh:
Dave Kreskowiak 23-Jul-17 12:40pm    
I can't stand coffee. Hmmmm... That might explain a few things.
OriginalGriff 23-Jul-17 12:02pm    
Not on that thread, and threading gets complicated.
What I'd suggest is that you look at using a timer to control when things happen instead of thinking in terms of functional programming - Windows is entirely event driven, and an event based solution will be a lot easier in the long run.
You may find many tutorials on 'making a simple animation with visual basic', just using Google. See, for instance How To: Basic Movement And Typing Animations In VB.Net - VB.NET Tutorials | Dream.In.Code[^].
 
Share this answer
 
You can use Application.DoEvents to give the UI thread a chance to catch up with itself.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click




    For i As Integer = 1 To 5

        System.Threading.Thread.Sleep(1000)
        TextBox1.Text = "/"
        Application.DoEvents()
        System.Threading.Thread.Sleep(1000)
        TextBox1.Text = "\"
        Application.DoEvents()
    Next




End Sub


I would refer you to Is DoEvents Evil?[^] or similar for the inevitable debate on whether that is good practice or not.
 
Share this answer
 
Comments
Dave Kreskowiak 24-Jul-17 19:05pm    
NOPE, NOPE, NOPE, NOPE, NOPE. Application.DoEvent is a BAD IDEA.

You must be very careful with using that because you can end up in a situation where your event handler, and any code it kicks off, can be triggered again, even though your code doesn't/may not support being re-entrant. It can make your code a pain in the ass to debug and an even bigger pain to replicate problems users report.

I haven't used it, nor even encountered the need for it, in 16 years. There is no need for it if you've written your code correctly.

IMHO, TPL has made the use of DoEvents inexcusable.
Lockwood 25-Jul-17 6:42am    
As I said, it is a solution that warrants a link to a discussion on the pros and cons of it.

For very simple UI thread tricks, it is a very useful tool. for more complex stuff, it is not the safest tool to use.

If the OP does just want to spin some text on screen on a timer, DoEvents is safe and full threading is overkill. If OP is wanting to do more than that then other alternatives need to be looked into.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900