Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : Show dialog on UI thread from another thread

I'm new to C#, but i've done a lots of java. Here's my problem : I'm trying to open a "SaveFileDialog" from a thread that is not the UI thread.

This is exactly what I try to do:

public partial class Form1: Form
{
    public string AskSaveFile()
    {
        var sfd = new SaveFileDialog();
        sfd.Filter = "Fichiers txt (*.txt)|*.txt|Tous les fichiers (*.*)|*.*";
        sfd.FilterIndex = 1;
        sfd.RestoreDirectory = true;
        DialogResult result = (DialogResult) Invoke(new Action(() => sfd.ShowDialog(this)));
        if(result == DialogResult.OK)
        {
            return sfd.FileName;
        }

        return null;
    }
}

This method will always be called from a thread different from the one who owns the Form. The problem is that when I execute this code, the "Form1" freeze and the "SaveFileDialog" doesn't show up.

Do you have some clue to help me to show the dialog from an independant thread?

like image 479
Moinonime Avatar asked Aug 27 '11 13:08

Moinonime


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 ...

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.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

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.


2 Answers

Make it look like this:

    public string AskSaveFile() {
        if (this.InvokeRequired) {
            return (string)Invoke(new Func<string>(() => AskSaveFile()));
        }
        else {
            var sfd = new SaveFileDialog();
            sfd.Filter = "Fichiers txt (*.txt)|*.txt|Tous les fichiers (*.*)|*.*";
            sfd.FilterIndex = 1;
            sfd.RestoreDirectory = true;
            return sfd.ShowDialog() == DialogResult.OK ? sfd.FileName : null;
        }
    }

If you still get deadlock then be sure to use the debugger's Debug + Windows + Threads window and look at what the UI thread is doing. Control.Invoke() cannot complete unless the UI thread is idle and pumping the message loop. Waiting for the worker thread to finish is always going to cause deadlock.

Also consider that this kind of code is risky, the user might not expect this dialog to suddenly show up and accidentally close it while mousing or keyboarding in the window(s) owned by the UI thread.

like image 158
Hans Passant Avatar answered Oct 11 '22 15:10

Hans Passant


Try this:

public partial class Form1: Form
{
    public string AskSaveFile()
    {
        if (this.InvokeRequired)
        {
            Invoke( new MethodInvoker( delegate() { AskSaveFile(); } ) );
        }
        else
        {
            var sfd = new SaveFileDialog();
            sfd.Filter = "Fichiers txt (*.txt)|*.txt|Tous les fichiers (*.*)|*.*";
            sfd.FilterIndex = 1;
            sfd.RestoreDirectory = true;
            if(sfd.ShowDialog() == DialogResult.OK) return sfd.FileName; 
        }               
        return null;
    }
}
like image 24
Marco Avatar answered Oct 11 '22 15:10

Marco