Soorena Aban
Soorena Aban

Reputation: 730

how to make form update while function is working

I have a function that attempt to connect to a server and post information about the progress and result. but i just found out that while my function is working the application will "freeze" and since the whole connecting thing might take a while the user might think that the application is malfunctioning and try to quit. do you know how to get the function to update form before continuing the rest of function, so the user know the application is working.

private void TestConnection()
{
    ResutBoxTable.Controls.Clear();
    ResutBoxTable.RowStyles.Clear();
    PrintResults("Starting connection test...", Color.White);
    ImapClient client = new ImapClient(IncAddressTxt.Text, true, certificateCheckbox.Checked);
    if (client.Connect())
    {
        PrintResults("Connection to incoming mail server succesful!",Color.Green );
        PrintResults("Attempting to Login to incoming mail server...", Color.White);
        if (client.Login(UserTxt.Text,PasswordTxt.Text))
        {
            PrintResults("Login to incoming mail server succesful succesfull!",Color.Green);
        }
        else
        {
            PrintResults("Failed to login to incoming mail server!", Color.Red);
            PrintResults("Please check your login information and try agian.", Color.Red);
        }
    }
    else
    {
        PrintResults("Failed to connect to incoming mail server!", Color.Red);
        PrintResults("Please check your connection prameters and try again.", Color.Red);
    }
    PrintResults("Testing connection complete!",Color.White);
}

also if your answer has something to do with threading please throw 1 or 2 explanation lines as i only know threading theoretically and never used it myself. thanks for your attention.

Upvotes: 2

Views: 342

Answers (1)

SILENT
SILENT

Reputation: 4278

The reason your application is freezing is cause everything is running on the UI thread. If you separate your busy function to a separate thread, the freezing won't occur.

Use Background Worker Threads to accomplish this. Another great example: Background thread for beginners


Used the second links example as source

    BackgroundWorker m_oWorker;

    public Form1()
    {
        InitializeComponent();
        m_oWorker = new BackgroundWorker();

        // Create a background worker thread that ReportsProgress &
        // SupportsCancellation
        // Hook up the appropriate events.
        m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);

        //optional progress indicators
        m_oWorker.ProgressChanged += new ProgressChangedEventHandler
                (m_oWorker_ProgressChanged);
        m_oWorker.WorkerReportsProgress = true;
    }

    void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        //Run your primary function
        TestConnection();

        //Progress indicator - optional
        m_oWorker.ReportProgress(100);
    }
    //optional
    void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        //Report progress

    }

Also, as a side note, you need to make PrintResults Thread friendly or use the Invoke/BeginInvoke hack.

Upvotes: 2

Related Questions