views:

866

answers:

1

howzit!

I'm a web developer that has been recently requested to develop a Windows forms application, so please bear with me (or dont laugh!) if my question is a bit elementary.

After many sessions with my client, we eventually decided on an interface that contains a tabcontrol with 5 tabs. Each tab has a datagridview that may eventually hold up to 25,000 rows of data (with about 6 columns each). I have successfully managed to bind the grids when the tab page is loaded and it works fine for a few records, but the UI freezes when I bound the grid with 20,000 dummy records. The "freeze" occurs when I click on the tab itself, and the UI only frees up (and the tab page is rendered) once the bind is complete.

I communicated this to the client and mentioned the option of paging for each grid, but she is adament w.r.t. NOT wanting this. My only option then is to look for some asynchronous method of doing this in the background. I don't know much about threading in windows forms, but I know that I can use the BackgroundWorker control to achieve this. My only issue after reading up a bit on it is that it is ideally used for "long-running" tasks and I/O operations.

My questions:

  1. How does one determine a long-running task?
  2. How does one NOT MISUSE the BackgroundWorker control, ie. is there a general guideline to follow when using this? (I understand that opening/spawning multiple threads may be undesirable in certain instances)
  3. Most importantly: How can I achieve (asychronously) binding of the datagridview after the tab page - and all its child controls - loads.

Thank you for reading this (ahem) lengthy query, and I highly appreciate any responses/thoughts/directions on this matter!

Cheers!

+1  A: 
  1. There's no hard and fast rule for determining a long-running task. It's something you have to know as a developer. You have to understand the nature of your data and your architecture. For example, if you expect to fetch some info from a desktop database with a single user from a table that contains a couple dozen rows you might not even bother showing a wait cursor. But if you're fetching hundreds of rows of data across a network to a shared database sever then you'd better expect that it will potentially be a long-running task to be handled not simply with a wait cursor but a thread that frees up your UI for the duration of the fetch. (You're definitely on the right track here.)

  2. BackgroundWorker is a quick and dirty way of handling threading in forms. In your case, it will very much tie the fetching of data to the user interface. It is doable, works fine but certainly is not considered "best practice" for threading, OOP, separation of concerns etc. And if you're worried about abusing the alocation of threads you might want to read up on the ThreadPool.

  3. Here's a nice example of using asynchronous threading with the thread pool. To do data binding, you fetch your data in the thread and when you get your callback, simply assign the result set to the the grid view's datasource property.

Paul Sasik
Hey psasik! thanks4 the input and 4 the links! I agree with your 1st point. Regarding your 2nd point, what then would be a real-world application for the BackgroundWorker component? What if if I had other elements on my form (say 6 items that each run off and do different things) that needed to be handled in an asynchronous manner? These aren't regarded as long-running tasks, but surely would warrant some sort of background execution.Another example would be a splash screen that loads the main form in the background.
dcryptd
dcrypt, there's nothing wrong with the idea of a background worker thread whatsoever. It's a worker thread, period. The only problem i have with it is the way Microsoft made it a design-time component which binds it so closely to your UI... This breaks several best practices rules because any change to your UI would have an effect on the thread logic and vice versa. There's a lack of separation similar to suing data connectivity components. You bind a form to a particular database... It all works and is used frequently in the real world, it's just not considered the best thing to do.
Paul Sasik
thanks 4your concise reasoning there. U've given me quite a bit 2think about, and its much appreciated!
dcryptd