Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

WPF – C# – Resize Buttons in parallel Thread

I have developed a little tool for my company. It’s an WPF-Application. The aim is to open a pdf file from a specific folder, do some work and as soon as you saved the file, the application should enable some buttons to continue the work.

So my idea was to create a new thread, checking all view seconds if the LastWriteTime for the FileInfo was changed. As soon as that happens, the thread should enable all buttons which are not enabled yet.

privat FileInfo file; //File selected


private void Open_PDF(FileInfo file)
{
    if (file == null) return;
    Process.Start(file.FullName); //Open with standard pdf editor
    new Thread(New ThreadStart(Wait_FileSaved)).Start();
}


private void Wait_FileSaved()
{
    while (file.LastWriteTimeUtc.Equals(new FileInfo(file.FullName).LastWriteTimeUtc)
    {
        Thread.Sleep(2000);
    }
    
    Console.WriteLine("File saved"); //Just to check if it works
    EnableButtons(btnChecked, btnDenied, btnClarify, btnPass);
}


private void EnableButtons(params Button[] buttons)
{
    foreach (Button button in buttons)
        button.IsEnabled = true;    //Code fails here, buttons are created in another thread
                            //so I don't have access to the Buttons in the parallel thread
}

I have already found some results at google for this problem. According to the solutions there, I have changed my Code for EnableButtons:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private bool EnableButtons(params Button[] buttons)
{
    if (!this.Dispatcher.CheckAccess())
        return (bool)this.Dispatcher.Invoke((Func<Button[],bool>)EnableButtons, buttons);
    foreach (Button button in buttons)
        button.IsEnabled = true;
    return true;
    
}

I am pretty sure I use Dispatcher class wrong. Do I have to create one before threading?
Is the code possible without threading? (and headache ^^)

You don’t have to post Documentation for Dispatcher class, I’ve already read this, and still have no clue what this class is for and even if it is the right class to use in this case 🙂

Thanks guys,
Micha

>Solution :

Avoid using new threads when dealing with UI. Instead, just leverage async/await as follows:

private void Open_PDF(FileInfo file)
{
    if (file == null) return;
    Process.Start(file.FullName); //Open with standard pdf editor
    //new Thread(New ThreadStart(Wait_FileSaved)).Start();
    Wait_FileSaved();
}


private async Task Wait_FileSaved()
{
    while (file.LastWriteTimeUtc.Equals(new FileInfo(file.FullName).LastWriteTimeUtc)
    {
        await Task.Delay(2000);
    }
    
    Console.WriteLine("File saved"); //Just to check if it works
    EnableButtons(btnChecked, btnDenied, btnClarify, btnPass);
}


private void EnableButtons(params Button[] buttons)
{
    foreach (Button button in buttons)
        button.IsEnabled = true;    //Code fails here, buttons are created in another thread
                            //so I don't have access to the Buttons in the parallel thread
}

By using the async/await (Task), you won’t block the UI thread.

Note that this is a super-simplified solution: definitely not the one I’d use in an app. However, I wouldn’t use a while/delay to observe the result. Instead, I would use the Process termination event.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading