2

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.

CubanGT
  • 351
  • 3
  • 11
  • 1
    Put your ParseFilteredData in your FormLoad event. – Frank Ball Apr 06 '20 at 19:54
  • 1
    @Frank Ball what do you mean? formload event for the main window? or the processing window? – CubanGT Apr 06 '20 at 20:30
  • 1
    Put it in the FormLoad event of your "Processing" form. If that's the only place where this function is called from, then put the entire function in the FormLoad event, if not, then create a simple class for it. If needed, you can simply pass in any needed parameters in the form's constructor. – Frank Ball Apr 06 '20 at 20:36
  • Related: [Async ShowDialog](https://stackoverflow.com/questions/33406939/async-showdialog). You can remove all code from the `Processing` form, and just use Noseratio's `ShowDialogAsync` method from inside the `btnFilters_Click` handler (after adding the `async` keyword to the handler). This way everything will be in one place. – Theodor Zoulias Apr 06 '20 at 23:02

1 Answers1

2

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.

CubanGT
  • 351
  • 3
  • 11
  • If you're building/populating multiple DataTables, it may be worth it to spread out the load among multiple threads. Take your heavy-hitters and let them run separately. Multi-threading this kind of process can save a lot of time. – Frank Ball Apr 06 '20 at 23:01