Winforms C# - Display loading form

Asked

Viewed 530 times

1

I own a main Winforms and would like while the asynchronous task is processing a new form (with GIF) is displayed, this would only close upon completion of processing.

             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 run the application does not process the instructions following the form view line:

_frmProcessor.ShowDialog();

The system stands still at this point. Some way to get around this problem?

  • friend it only runs back when the _frmProcessor is closed. to continue running use . show

  • The execution is continuing, thanks for the reply. I would not have thought that this method blocked execution with the main form.

  • We’re here to help each other. =)

2 answers

1

Hello, For long term processing... In order for your auxiliary form to continue running Gif during processing you will need to start it in a Thread other than Thread where the processing will be done.

Follow the example of the auxiliary 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)
        {
            //
        }
    }
  • AP processing is already done asynchronously and therefore does not block the user-interface. Summarizing your answer is a worse alternative to AP

  • We’re here to help each other, being kind to each other is also important Bruno.

0


The program executed perfectly with the change of the method.

_frmProcessor.Show();

Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.