Threading in .NET 4

Posted on 2011-10-27
Last Modified: 2012-05-12
I have a scenario as follow in Windows Product based application using .net 4.
1. A VB.NET project consists of one usercontrol with a few controls in it.
2. I load this control (once or many) times on the main form as in Me.Controls.Add(usercontrol)
3. The project has a timer which updates a label control in the usercontrol for every tick (every 1000 ms) which gets initiated in the Main form
4. I do also have a button on the main form with a separate task say Thread.Sleep or loop untill the elapsed time using stopwatch crosses 5000 ms and then comes out of the button click event
5. Now When I click this button my control which is getting updated by a timer in the usercontrol ALSO stops untill the button click event completes. It is as if the entire application gets suspended for a while (5 secs).

NOW I would need the timer control to update the label control in the usercontrol IRRESPECTIVE of any process that is running through the main form.
I would need a seamless update to the label in the user control.

I have tried with multiple threads et al. Nevertheless whenver the main form or the main thread does some job then the other controls say a timer update etc...stops for a while until the main form is free.

I would like to know how to keep updating the label control in the user control (say a timer updates the elapsed time in the label control) even when I click the button from the main form which does a loop for 5 seconds etc....

Please do let me know how to go about it.

Thank You
Question by:ravindrasen
    LVL 85

    Accepted Solution

    In #4, you are locking up the main UI thread with your Sleep() call and/or looping structure, preventing the labels from being updated.

    I think you want a five second delay within your button handler?
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' ... some code ...
            Dim dt As DateTime = DateTime.Now.AddSeconds(5)
            While dt > DateTime.Now
                System.Threading.Thread.Sleep(100) ' <-- SMALL delay to reduce CPU usage
                Application.DoEvents() ' <-- Keep the app responsive during the pause
            End While
            ' ... some more code ...
        End Sub

    Open in new window


    Author Closing Comment

    Good, had a tough time with the UI responsiveness in .NET 4 and win 7.

    Thanks Idle_Mind

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    745 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