CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2008
    Posts
    161

    Pause and continue a foreach loop.

    I have a foreach loop running on a separate thread.

    Code:
    foreach ( string str in strg )
    {
    // my action 
    }
    I was wondering if there was a way i could pause this foreach loop at some point and then resume it later.

    I tried searching this, but all i come up with is beginner tutorials on how to use the foreach loop.

    So can anyone show me a different way to do an action so i can pause and resume it at some point, or do i just have to save where it stopped and resume it from there or something.

    Thanks.

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Pause and continue a foreach loop.

    Try to Sleep () for the duration you want to pause and after that it will continue to execute from where you left.

  3. #3
    Join Date
    Mar 2008
    Posts
    161

    Re: Pause and continue a foreach loop.

    Quote Originally Posted by vcdebugger View Post
    Try to Sleep () for the duration you want to pause and after that it will continue to execute from where you left.
    i can't use sleep because i want the user to be able to choose how long to pause it for, basicly i want to pause it forever until the user says do it again from where it left off.

  4. #4
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Pause and continue a foreach loop.

    write you own delay function which takes the number of seconds/mins/hours as parameter which is the input provided by the user as to how much duration he need to PAUSE.

    By taking that input parameter call another function Delay(int time) - there write a dummy for loop with Zero operation till you want to come out -

    Code:
    void Delay( int time)
    {
    
    for (int i=0 ; i < time*seconds ;i++)
        if( some check for the counter i)
          return;    
    
    }

  5. #5
    Join Date
    Mar 2008
    Posts
    161

    Re: Pause and continue a foreach loop.

    Quote Originally Posted by vcdebugger View Post
    write you own delay function which takes the number of seconds/mins/hours as parameter which is the input provided by the user as to how much duration he need to PAUSE.

    By taking that input parameter call another function Delay(int time) - there write a dummy for loop with Zero operation till you want to come out -

    Code:
    void Delay( int time)
    {
    
    for (int i=0 ; i < time*seconds ;i++)
        if( some check for the counter i)
          return;    
    
    }
    Quote Originally Posted by Pale
    i can't use sleep because i want the user to be able to choose how long to pause it for, basicly i want to pause it forever until the user says do it again from where it left off.
    I don't want to ask the user how long he wants to pause, i was suggesting maybe he pause it forever, and then come back and resume it after he has finished something else.

  6. #6
    Join Date
    Jul 2006
    Posts
    297

    Re: Pause and continue a foreach loop.

    Try using the ManualResetEvent class
    Just add a progress bar called "progress" and a button called "toggle" that fires the toggle_Click function whenever its clicked and you can see this code in action.

    When i click toggle it pauses the progress bar from updating. When i click it again it resumes. Hope this helps. The Thread.Sleep() is just there to simulate some type of processing so it doesn't complete right away.

    Code:
            public Boolean Stop = true;
            public ManualResetEvent Reset = new ManualResetEvent(true);
    
            public Form1()
            {
                InitializeComponent();
                ThreadPool.QueueUserWorkItem(StartNewThread, Reset);
            }
    
            private void toggle_Click(object sender, EventArgs e)
            {
                if (Stop)
                    Reset.Reset();
                else
                    Reset.Set();
    
                Stop = !Stop;
            }
    
            public void StartNewThread(Object state)
            {
                ManualResetEvent reset = (ManualResetEvent)state;
                for (Int32 i = 0; i < 100; i++)
                {
                    progress.Invoke(new MethodInvoker(delegate { progress.Increment(10); }));
    
                    Thread.Sleep(500);
                    reset.WaitOne();
                }
            }
    Last edited by monalin; July 30th, 2009 at 09:43 AM.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Pause and continue a foreach loop.

    Pale. You can't interrupt the main UI thread because it will become unreponsive. The UI thread must never block (or only block for short periods).

    VCProgrammer. Don't do this. This kind of programming just wastes cpu cycles.

    Monalin has the right idea. If you need to perform some functionality that you need to pause, you'll need to do this in another thread, then trigger the start/stop/pause/resume functionality from the ui thread using an event (or multiple events).

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