CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2006
    Posts
    99

    Unhappy long loop problem

    hello guys,

    i'm writing a windows application. once the user click on a button, i will run a loop. the loop will take a long time to finish, and during the execution the program looks like dead. i want my application can still response to user input, what do i do?

    thanks.

    plus, i saw a warning every time i run the program saying that the program doesn't respond. everything is actually ok, i just click continue, everything will be fine. but the warning is still annoying. how should i deal with that.

    the warning shows up when i debug the code. i don't know if it will still show up if i compile it as a release mode.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: long loop problem

    This is quite simple,
    just use the BackgroundWorker Thread, or the "usual" threads as follows:

    Code:
    using System;
    using System.Text;
    using System.Windows.Forms;
    
    using System.Threading;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            private void MyThread()
            {
                // do something loop-ish here
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Thread thread = new Thread(new ThreadStart(MyThread));
                thread.Start();
            }
    
        }
    }

    The thread runs independently and does not freeze your GUI.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: long loop problem


  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: long loop problem

    this tutorial is great, now I finally understand how the messages work, thx BigEd781
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured