user6061237
user6061237

Reputation:

Form does not show in C# while running a long loop?

My form does not show when I click a button but the program continues as I can see from the debug in C#(WindowsFormApp).

The button's code is:

private void button1_Click(object sender, EventArgs e)
    {
        string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        string files = string.Join(",", Directory.GetFiles(desktop, "*", SearchOption.AllDirectories).ToList().ToArray());
        textBox1.Text = files;
        string dir = string.Join(",", Directory.GetDirectories(desktop, "*", SearchOption.AllDirectories).ToList().ToArray());
        textBox2.Text = dir;
        int count;
        for (count = 0; count <= dir.Split(',').Length - 1; count++)
        {
            Console.WriteLine(dir.Split(',')[count].Replace(desktop, "").Replace('\\', '/'));
            mkdir(dir.Split(',')[count].Replace(desktop, "").Replace('\\', '/'));
        }
        for (count = 0; count <= files.Split(',').Length - 1; count++)
        {
            Console.WriteLine(files.Split(',')[count].Replace(desktop, "").Replace('\\', '/'));
            upload(files.Split(',')[count], files.Split(',')[count].Replace(desktop, "").Replace('\\', '/'));
        }
    }

where mkdir and upload is defined to create and upload files using ftp. Is there a way I can still see the form while the loops are running.

Note: I can view the form again when the loops finish.

Edit: Many of the answers included were about multithreading and async usage. Can you please help how exactly I can implement that.

Upvotes: 0

Views: 130

Answers (5)

user6061237
user6061237

Reputation:

This Worked:

await Task.Run(() => fuction())

Upvotes: 0

Kent Kostelac
Kent Kostelac

Reputation: 2446

Firstly. You need to make your program more modular and separate UI tasks from program tasks. In your case you would have a class that does everything inside of what button1_Click() does. Also button1_click is not descriptive and it should be.

In the class you would define your methods as tasks and in your form class you would call the methods in the class using the await keyword.

For example in your Form.cs.

    private async void AsyncCall_Click(object sender, EventArgs e)
    {
        //ac is an object that is initialized in Form constructor.
        await ac.ChangeHello1();
    }

And in the AsyncClass.cs

class AsyncClass
{
    public string Hello = "ASYNC NOT DONE";
    public AsyncClass()
    {

    }

    private async Task<string> ChangeHello()
    {
        await Task.Yield();
        await Task.Delay(2000);
        return "ASYNC DONE";
    }

    public async Task ChangeHello1()
    {
        Hello = await ChangeHello();
    }
}

Now you can still click on other elements in the UI while it does it's computation.

Upvotes: 0

ankur goel
ankur goel

Reputation: 76

Your application is single threaded application. To make it multiple threaded, Use parallel task library or background worker thread.

Upvotes: -1

Martin Vich
Martin Vich

Reputation: 1082

Problem is that you're performing long running operations on the same thread which also runs UI.

See this SO question to as example how to run things async in WinForms.

SYNC vs ASYNC

Synchronous operations run in order always only one a time. Which means that while your code is busy uploading files to FTP it cannot handle any UI actions you do. Once the upload is done it's free to respond again

Asynchronous operations run parallel to each other so your UI doesn't get blocked by your upload operation

Upvotes: 3

Nicolas Takashi
Nicolas Takashi

Reputation: 1740

It could be happening because you are running in the main thread, you can put your code run in the other thread and release the main thread

Upvotes: 1

Related Questions