does button1_Click method need to be short?

Posted on 2011-04-28
Medium Priority
Last Modified: 2012-05-11
If my button1_Click method is going to do a whole lot of work that's going to take a long time, do I need to put it in it's own thread so button1_Click returns quickly so as not to block the rest of the application? Or is this all handled for me and my button1_Click method can take all the time it wants?
Question by:deleyd
  • 2
LVL 40

Accepted Solution

mrjoltcola earned 1000 total points
ID: 35484902
You are exactly right -- button_Click must return with reasonable time, or your GUI will hang.

You can do it, as you said, with threads, or using a delegate. The more ".NET" way would be to use delegate and BeginInvoke()

Google / MSDN delegate invoke or begininvoke asynchronous for more info.
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 1000 total points
ID: 35484941
I'm fond of using threads, but they can complicate matters slightly (specifically interacting with the GUI thead requires some additional steps, and access to shared resources requires synchronization).  Unless your click event handler is going to take a very long time, I might be inclined to just let it run - even if it means my GUI appears hung for a second or two.

My next step would be to consider using a BackgroundWorker on my form, as that will tend to some of the synchronization required for you, which will simplify your code a bit.
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35484946

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question