3

I have a child form in my application. This form has got about more than 50 comboboxes and everyone is getting data from database. All combobexs are loaded in the load event of the form. The data is large. Data retrieval takes about 2 minutes. when I open this form, my whole application becomes unresponsive. The application hangs and it gets life after about 2 minutes :/

As I have studied, we can use different threads in order to avoid such situations. Can someone guide, is it possible, safe and secure to implement multi-threading in order to make my application responsive?

Please guide me and write a sample code if possible how multithreading works in c#. You can simply explain using a form having a gridview that takes datatable as daTASOURCE in a separate thread and GUI is responsive even database takes too much time...Any help is appreciated.Thanx in advance!

4
  • You want to have asynchronous loading, while also trying hard not to have too many threads poke around the GUI. Have you tried initiating a simple thread to do that call for you, with a callback to notify you when it's ready?
    – Shark
    Commented Apr 26, 2013 at 10:55
  • One of the best tutorials about threading in .NET: yoda.arachsys.com/csharp/threads/index.shtml
    – jAC
    Commented Apr 26, 2013 at 10:57
  • Dear Shark, I have never implemented multi-threading. This is why i am asking here. how to implement? Is is safe n secure to implement? when it should not be implemented n all that? Can you send me a sample code that would fill a datatable from database and then will be assigned to a gruidview on the load event of the form. Even data retreival might take more than 2 minute, application shud be responsive... Commented Apr 26, 2013 at 10:58
  • @user2109843 When you have never implemented multithreading, then good choice will be some book. Buy some book.
    – Regfor
    Commented Apr 26, 2013 at 11:13

2 Answers 2

7

Take a look at the BackgroundWorker class. This can do exactly what you want. You can also include something like a progress bar to show users the data is still being loaded before they go ahead and do stuff in your child form.

3

Use task parallel library, which is included in .NET starting from version 4 or Parallel Extensions. Samples of using TPL you can find here

And read more about it here.:

http://bradwilson.typepad.com/blog/2012/04/tpl-and-servers-pt1.html

http://www.codeproject.com/Articles/30975/Parallel-Extensions-for-the-NET-Framework-Part-I-I

http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx

http://thenewtechie.wordpress.com/2012/01/03/introduction-to-tpl-part-1/

Reactive Exensions are a little bit harder. But anyway good. Samples here. Some introduction to it here:

http://www.codeproject.com/Articles/47498/A-quick-look-at-the-Reactive-Extensions-Rx-for-Net

http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/08/18/reactive-extensions-for-net-stuff-happens.aspx

Anyway you can find very easy more information.

Speaking about BackgroundWorker. It's good solution for winforms. But this approach is outdated. TPL or Rx are new approaches, more perfomant, more comfortable, especial when you have to much controls. Having so much controls with async operation is also a question to UI design, maybe it should be changed. Anyway BackgroundWorker also a good choice and it's up to you what select.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.