Mahdi Tahsildari
Mahdi Tahsildari

Reputation: 13602

Show form and fetch data on background meanwhile

I'm working on a winforms application that works with database a lot, and 90% of my forms need to load some initial data from database on Form_Load.

I've been told to use threading to show the form, and then go and fetch data and fill the form with data, otherwise the customer can't see the form until the postman goes to the db and comes back with data :)

I've done some simple tasks with threads before, but this case is a bit confusing to me. I have a mainForm which is the MdiParent and other forms are MdiChild like below:

ChildForm child = new ChildForm { MdiParent = Program.mainForm };

and my mainForm (the MdiParent is static in Program.cs)

I don't know whether I should use Thread? BackgroundWorker? Other solutions?

I would be way more than happy if your kind replies could help me through this case and empower my incomplete knowledge. Thanks.

Upvotes: 0

Views: 237

Answers (1)

Richard Schneider
Richard Schneider

Reputation: 35464

Using a BackgroundWorker is the easiest way to go. It handles the threading issues, catching exceptions and runs the work completion callback on the UI Thread.

If you are using C# 4.5, then await/asych is also a good approach.

Upvotes: 1

Related Questions