WinForms C # - Display upload form

1

I have a main WinForms and would like it to appear as long as the asynchronous task is processing, a new form (with GIF ) would only close when processing is finished.

             frmProcessor _frmProcessor = new frmProcessor();
            _frmProcessor.ShowDialog(); //Este é o formulário que é exibido enquanto a tarefa é executada.

            string account = await _manager.DoAccount(email, password);
            if (account != "")
            {
                ...
            }
            else
            {
                ...
            }
            _frmProcessor.Hide(); // Então o formulário é fechado

At runtime, the application does not process the following instructions in the form's display line:

_frmProcessor.ShowDialog();

The system is stopped at this time. Is there a way to get around this?

    
asked by anonymous 13.10.2016 / 18:39

2 answers

0

The program performed perfectly with the method change.

_frmProcessor.Show();

Thank you!

    
13.10.2016 / 18:50
1

Hello, For long processing ... In order for your helper form to continue running the Gif during processing it will be necessary to start it on a Thread other than the Thread where the processing will be done.

Here is the example of the helper form where your Gif image will be displayed:

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace Connessioni.Elos.DB.Remote.Controls
{

/// <summary>
/// Form padrão para ser exibido quando um processamento longo é iniciado
/// </summary>
public partial class FrWorking : Form
{

    /// <summary>
    /// Se o form foi iniciado
    /// </summary>
    public bool Started;

    /// <summary>
    /// Contrutor
    /// </summary>
    public FrWorking()
    {
        InitializeComponent();
    }

    /// <summary>
    /// Exibe o form como dialog
    /// </summary>
    public void ShowDiag()
    {
        try
        {
            Application.DoEvents();
            ShowDialog();
        }
        catch (Exception)
        {
            //
        }
    }

    /// <summary>
    /// Raises the <see cref="E:System.Windows.Forms.Form.Shown"/> event.
    /// </summary>
    /// <param name="e">A <see cref="T:System.EventArgs"/> that contains the event data. </param>
    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        Application.DoEvents();
        BringToFront();
        Started = true;
    }

}

}

Now follow the example of how to use this form:

    /// <summary>
    /// Carrega a.....
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btLoad_MouseUp(object sender, MouseEventArgs e)
    {
        //Form a ser exibido durante o processamento
        var w = new FrWorking();
        //Executando o form auxiliar em uma nova System.Threading.Thread
        var t = new Thread(w.ShowDiag);
        //Iniciando a Thread
        t.Start();

        //Aguardando até q a Thread seja iniciada
        while (!w.Started) { }

        //:) Execute seu processamento aqui, e seja feliz....

        try
        {
            //Fechando form auxiliar
            t.Abort();
        }
        catch (Exception)
        {
            //
        }
    }
    
14.10.2016 / 08:41