質問

I am using a OpenFileDialog to Open a File i want to process in my application, but the processing takes few seconds, and during that processing time the OpenFileDialog stays visible, and it's bothering!!! i want to hide my OpenFileDialog during the processing time ! Same problem goes to the SaveFileDialog

private void ofdImportation_FileOk(object sender, CancelEventArgs e)
        {
            Processing(); //Takes Few Seconds
            //ofdImportation remains visible during that time...
            //i want to hide it...
        }

thank you everybody...

役に立ちましたか?

解決

if (OpenFileDialog.ShowDialog() == DialogResult.Ok)
{
  // Do stuff 
}

The OK and Cancel events should be for UI specific behaviour irrespective of whatever the resulting file is for.

Separation of concerns

Clicking ok gives you a file, cancel gives you say a null, then you have a class with a process method that you pass the file name from the dialog to. It shouldn't care where the file name came from.

Think about the hurdles you'd have to leap to unit test Processing()

他のヒント

Assuming you have a button to open the Dialog

Solution 1 (this way the window freezes so its not really a solution, i post it anyway):

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    if (ofd.ShowDialog() == DialogResult.Ok)
    {
        Thread.Sleep(10000);
    }
}

Solution 2 (use of BackGroundWorker, a helpful tool for async jobs):

public partial class Form1 : Form
{
    BackgroundWorker bgw;
    String fileUrl;

    public Form1()
    {
        InitializeComponent();
        bgw = new BackgroundWorker();
        bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
        bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
    }

    void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        button1.Text = fileUrl;
    }

    void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        Thread.Sleep(10000);
    }


    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        var dlr = ofd.ShowDialog();
        if (dlr == DialogResult.Ok)
        {
            fileUrl = ofd.FileName;
            bgw.RunWorkerAsync();
        }
    }
}

edit: 'Thread.Sleep(10000)' simulates your long running process ('Processing();')

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top