Tech Off Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

are Loops in C# dangerous?

Back to Forum: Tech Off
  • User profile image
    Shark_M

    Hi guys,
        I have an application that has many loops, and sometimes these loops hang the application, is there a way as to let the application "breath" , as in doevents, while doing intesive loops?

    People tell me doevents is dangerous and should not be used unless really needed and in a calculated places in the code,

    so what should i do ?

  • User profile image
    JChung2006

    Try to refactor the code so that the work being done in your loops can be performed by another thread.

  • User profile image
    TommyCarlier

    You could perform the expensive functions on a separate thread. That way, the UI will remain responsive. In .NET 2.0 there's a new 'BackgroundWorker' class that makes this easier.

  • User profile image
    Shark_M

    OKay, So why did they maintain Application. DoEvents ()

    Can you tell me why they have it there in .NET framework, when it disrupts the flow of the program instructions? expecially with loops?
    dont i have to have it even in a worker thread in the background?

  • User profile image
    Andrew Davey

    I hope you have read http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsApplicationClassDoEventsTopic.asp

    DoEvents basically waits until all pending window messages have been processed. If your code is performing heavy loops that are locking the UI, then for goodness sake move it to another thread. Sprinkling DoEvents everywhere is NOT a good solution.

    Loops are no more "dangerous" than making a web service call in a button click event handler!

  • User profile image
    W3bbo

    Andrew Davey wrote:
    Loops are no more "dangerous" than making a web service call in a button click event handler!


    Of course, the whole "UI Events" paradigm with ASP.NET isn't well-suited and is more of a "lets dumb things down for the Windows.Forms devs who CBA to learn how webforms really work"

    So yes, it is rather dangerous, because then these devvies continue with this in mind and make horribly ineffective applications later on.

  • User profile image
    Andrew Davey

    Erm, I wasn't refering to ASP.NET at all. I was talking about calling a web service from a Windows app and blocking the UI thread while you wait for the web server to return.

    In short: Blocking the UI thread is bad.

  • User profile image
    W3bbo

    Andrew Davey wrote:
    Erm, I wasn't refering to ASP.NET at all. I was talking about calling a web service from a Windows app and blocking the UI thread while you wait for the web server to return.

    In short: Blocking the UI thread is bad.


    My bad, I misread your post Smiley

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.