Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Code processing order - strange behaviour

Tags:

c#

winforms

I have the follow button click event:

private void btnRun_Click(object sender, EventArgs e)
    {
        label1.Visible = true;

        if (SelectDatabase())
        {
            if (string.IsNullOrEmpty(txtFolderAddress.Text))
                MessageBox.Show("Please select a folder to begin the search.");
            else
            {

                if (cbRecurse.Checked == false || Directory.GetDirectories(initialDirectory).Length == 0)
                {
                    CheckSingleFolder();
                }
                else
                {
                    CheckSingleFolder();
                    directoryRecurse(initialDirectory);
                }

                                }
        }


    }

Effectively, it does a few checks and then starts some directory recursion looking for specific files. However, the very first line of code to make the label visible does not occur until after the directories have been recursed? Anybody know why this would be?

Thanks.

like image 722
Darren Young Avatar asked Apr 04 '11 13:04

Darren Young


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

You're doing everything within the UI thread, which is a really bad idea - the UI doesn't get to update, react to events etc until you've finished.

You should use a background thread and update the UI with progress etc using Control.BeginInvoke, or perhaps use a BackgroundWorker.

Basically, there are two golden rules in WinForms (and similar with WPF/Silverlight):

  • Don't do anything which can take a significant amount of time in the UI thread
  • Don't touch any UI elements from any thread other than the UI thread
like image 192
Jon Skeet Avatar answered Sep 23 '22 21:09

Jon Skeet