does button1_Click method need to be short?

Posted on 2011-04-28
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
    LVL 40

    Accepted Solution

    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
    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

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now