0

i have one form which is doing some long process on form load event, So i want to show One Gif image "Please Wait" during form load event.

below is code.

private void frmWaitShow()
        {
            try
            {
                frmWaitwithstatus objWait = new frmWaitwithstatus();// this form has Gif Image for Processing
                objWait.lblStatus.Text = "Processing Request, Please wait...";
                objWait.ShowDialog();
            }
            catch (Exception ex)
            {
                Logger.SystemException(ex);
                Logger.FTSError(" ERROR :" + ex.Message + "frmTest || frmWaitShow");

            }
        }


        Thread oThread;
        private void frmTest_Load(object sender, EventArgs e)
        {
            try
            {


                oThread = new Thread(new ThreadStart(frmWaitShow));
                oThread.Start();

                //Functions for Connection with devices
                if (LoadDatafromDB() == false) return;
                if (ElectTestLoad() == false) return;
                if (PowerOnSelfTest() == false) { return; }
                InitiControlsElectTest();
                SetSystemMode(SystemMode.ElectricalMode);

                oThread.Abort();
            }
            catch (Exception ex)
            {
                oThread.Abort();
                Logger.SystemException(ex);

            }
        }

after Thread.start() my debugger go one one step in each thread main and one i created but after it go to below line.

frmWaitwithstatus.cs constructor first line

public frmWaitwithstatus()

it stop execute my Thread and execute all function of main thread once Main Thread execution complete after then only it start execute my thread (which is Gif processing image).

Aryan Firouzian
  • 1,940
  • 5
  • 27
  • 41
VARUN NAYAK
  • 656
  • 5
  • 15
  • 36
  • Use Tasks and follow the async/await path – Sir Rufo Nov 04 '17 at 08:55
  • Possible duplicate of [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) –  Nov 04 '17 at 09:10
  • @MickyD No its not. i know how to handle UI controls on different Thread, here i am calling separate form which has GIF image from the thread. – VARUN NAYAK Nov 04 '17 at 09:27
  • You clearly don't with your code riddled with so many mistakes. You are clearly attempting to update UI from a worker thread; showing a dialog from a MTA thread; blocking your worker thread and last but not least, ending the thread ungratefully by calling abort. –  Nov 04 '17 at 10:45
  • [Show Loading animation during loading data in other thread](https://stackoverflow.com/a/39142535/3110834) – Reza Aghaei Nov 04 '17 at 18:36
  • [Show Transparent Loading Spinner above other Controls](https://stackoverflow.com/a/37473192/3110834) – Reza Aghaei Nov 04 '17 at 18:37

1 Answers1

1

Using the async/await pattern will made this an easy task and every form will work on UI thread:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private async void Form1_Load(object sender, EventArgs e)
    {
        // async show loading form dialog
        var loadingForm = new LoadingForm();
        var loadingDialogTask = this.InvokeAsync(loadingForm.ShowDialog);

        // async loading data
        var data = await LoadDataAsync();
        listBox1.DataSource = data;

        loadingForm.Close();
        await loadingDialogTask;
    }

    private async Task<ICollection<string>> LoadDataAsync()
    {
        // fake work load
        await Task.Delay(4000).ConfigureAwait(false);
        return Enumerable.Range(1,20000).Select(e => e.ToString()).ToList();
    }

}

Needed async extension for the controls:

public static class ControlAsyncExtensions
{
    public static Task InvokeAsync(this Control control, Action action)
    {
        var tcs = new TaskCompletionSource<bool>();
        control.BeginInvoke(new Action(() =>
        {
            try
            {
                action();
                tcs.SetResult(true);
            }
            catch (Exception ex)
            {
                tcs.SetException(ex);
            }
        }
        ));
        return tcs.Task;
    }

    public static Task<T> InvokeAsync<T>(this Control control, Func<T> action)
    {
        var tcs = new TaskCompletionSource<T>();
        control.BeginInvoke(new Action(() =>
        {
            try
            {
                tcs.SetResult(action());
            }
            catch (Exception ex)
            {
                tcs.SetException(ex);
            }
        }
        ));
        return tcs.Task;
    }
}
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73