-
July 30th, 2009, 12:21 AM
#1
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.
-
July 30th, 2009, 12:30 AM
#2
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.
-
July 30th, 2009, 02:59 AM
#3
Re: Pause and continue a foreach loop.
Originally Posted by vcdebugger
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.
-
July 30th, 2009, 04:18 AM
#4
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;
}
-
July 30th, 2009, 06:51 AM
#5
Re: Pause and continue a foreach loop.
Originally Posted by vcdebugger
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;
}
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.
-
July 30th, 2009, 09:41 AM
#6
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.
-
July 30th, 2009, 04:53 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|