Trying to add a please wait popup when executing a long process, pop up shows, but process never runs

Viewed 235

So i have a process that runs for a bit with larger files and looking to add a "Please Wait.... Processing... popup so that know its working on their tasks.

I followed this video example on youtube C# Wait Form Tutuorial

Implemented just like the example, but but method that actually does the work never runs

This is my processing form logic, which does display when i run the whole application

public partial class Processing : Form
{
    public Action Worker { get; set; }

    public Processing(Action worker)
    {            
        InitializeComponent();
        if(worker==null)
            throw new ArgumentNullException();
            Worker = worker;
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        Task.Factory.StartNew(Worker).ContinueWith(t => { this.Close(); }, TaskScheduler.FromCurrentSynchronizationContext());
    }
}

Then this is how i call it from my button click

    private void btnFilters_Click(object sender, EventArgs e)
    {
        using (Processing pc = new Processing(ParseFilteredData))
        {
            pc.ShowDialog(this);
        }
        //ParseFilteredData();
    }

Why is my ParseFilteredData executing but not updating my main form? I stepped thru the code and it executes the method, but my UI doesnt reflect the data.

Please keep in mind that my ParseFilteredData works, if you comment out the using statement and just run the method on button click, after a few minutes, all my listboxes are populated.

1 Answers

So i made a few changes to my code, i moved all the datasource binding out of my ParseFilteredData method into its own, so the ParseFilteredData now just builds the datatables, which is the time consuming part. Then after the Using statement i call my new method and everything populates and shows up

    private void btnFilters_Click(object sender, EventArgs e)
    {
        using (Processing pc = new Processing(ParseFilteredData))
        {
            pc.ShowDialog(this);
        }
        BindAllListBoxes();
    }

I also moved the datatables outside the method, and just populated them instead of initializing and populating all at the same time. This allowed the datatables to be accessible outside that method.

So for my code to work as i wanted, i had to split my ParseFilteredData method. It seems to work like a charm now..

Thank you Frank for the suggestion.

Related