469,314 Members | 2,208 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,314 developers. It's quick & easy.

Button to interupt a foreach loop?

Hi all,

New to this group and to C# in general (experienced in MFC). Anyway I have
a ListView and a foreach loop that is doing something to each item in the
list and updating the list on the screen. I have a button on the screen
that I'll call STOP that is supposed to stop the processing of the loop in
midstream. I have a bool global variable call bStop. The OnClick for the
stop button sets bStop to true. The loop checks the value of bStop at the
beginning of each loop and calls break if it is true. Problem is bStop is
never set to true until after the loop finishes. In C++ I could make a call
to a function that would use PeekMessage to loop through the message que and
Dispatch any Dialog messages so that the bStop value would be updated.
Don't know how to do this in C#. Any ideas.

Thanks in advance,
Dave

Nov 16 '05 #1
6 2985

(1) You could do the work on another thread (Also see Control.Invoke to
update the GUI from the other thread.
For a start, I just googled this:
http://samples.gotdotnet.com/quickst...rshalling.aspx

(2) Simpler - I think there's a DoEvents (Under Application?) -- call this
in your loop, a la down-home VB-style cookin'.

m

"Dave Spencer" <sp*******@fuse.net> wrote in message
news:C4***************@fe37.usenetserver.com...
Hi all,

New to this group and to C# in general (experienced in MFC). Anyway I have a ListView and a foreach loop that is doing something to each item in the
list and updating the list on the screen. I have a button on the screen
that I'll call STOP that is supposed to stop the processing of the loop in
midstream. I have a bool global variable call bStop. The OnClick for the
stop button sets bStop to true. The loop checks the value of bStop at the
beginning of each loop and calls break if it is true. Problem is bStop is
never set to true until after the loop finishes. In C++ I could make a call to a function that would use PeekMessage to loop through the message que and Dispatch any Dialog messages so that the bStop value would be updated.
Don't know how to do this in C#. Any ideas.

Thanks in advance,
Dave

Nov 16 '05 #2
You'll have to put your for loop in another thread. Either one explicitly
created by you, or a pool thread created via an asynchronous delegate. The
main thread can set the bool that the for loop can check and exit when set.

good luck,
kevin aubuchon
"Dave Spencer" <sp*******@fuse.net> wrote in message
news:C4***************@fe37.usenetserver.com...
Hi all,

New to this group and to C# in general (experienced in MFC). Anyway I have a ListView and a foreach loop that is doing something to each item in the
list and updating the list on the screen. I have a button on the screen
that I'll call STOP that is supposed to stop the processing of the loop in
midstream. I have a bool global variable call bStop. The OnClick for the
stop button sets bStop to true. The loop checks the value of bStop at the
beginning of each loop and calls break if it is true. Problem is bStop is
never set to true until after the loop finishes. In C++ I could make a call to a function that would use PeekMessage to loop through the message que and Dispatch any Dialog messages so that the bStop value would be updated.
Don't know how to do this in C#. Any ideas.

Thanks in advance,
Dave

Nov 16 '05 #3
Hi Dave,

Can you try inserting an Application.DoEvents() in your foreach loop?

Michel

"Dave Spencer" <sp*******@fuse.net> wrote in message news:<C4***************@fe37.usenetserver.com>...
Hi all,

New to this group and to C# in general (experienced in MFC). Anyway I have
a ListView and a foreach loop that is doing something to each item in the
list and updating the list on the screen. I have a button on the screen
that I'll call STOP that is supposed to stop the processing of the loop in
midstream. I have a bool global variable call bStop. The OnClick for the
stop button sets bStop to true. The loop checks the value of bStop at the
beginning of each loop and calls break if it is true. Problem is bStop is
never set to true until after the loop finishes. In C++ I could make a call
to a function that would use PeekMessage to loop through the message que and
Dispatch any Dialog messages so that the bStop value would be updated.
Don't know how to do this in C#. Any ideas.

Thanks in advance,
Dave

Nov 16 '05 #4
I inserted a DoEvents() into the loop just before I check for the stop
variable as a couple of you have suggested. This never works if I hit the
stop button once but always works if I hit the stop button a second time.
Not sure I'm smart enough to implement one of the multiple thread solutions
but I may give it a try if I can't get this to work.

Thanks,
Dave
"fd123456" <fd******@hotmail.com> wrote in message
news:c8**************************@posting.google.c om...
Hi Dave,

Can you try inserting an Application.DoEvents() in your foreach loop?

Michel

"Dave Spencer" <sp*******@fuse.net> wrote in message

news:<C4***************@fe37.usenetserver.com>...
Hi all,

New to this group and to C# in general (experienced in MFC). Anyway I have a ListView and a foreach loop that is doing something to each item in the list and updating the list on the screen. I have a button on the screen that I'll call STOP that is supposed to stop the processing of the loop in midstream. I have a bool global variable call bStop. The OnClick for the stop button sets bStop to true. The loop checks the value of bStop at the beginning of each loop and calls break if it is true. Problem is bStop is never set to true until after the loop finishes. In C++ I could make a call to a function that would use PeekMessage to loop through the message que and Dispatch any Dialog messages so that the bStop value would be updated.
Don't know how to do this in C#. Any ideas.

Thanks in advance,
Dave


Nov 16 '05 #5
Hi Dave,

Instead of inserting DoEvents() right before you check for the Stop
variable, insert it at the top of your method. DoEvents takes some
processing time and is executed (I believe) on a separate thread,
which means your check is executed concurrently with the beginning of
the events polling.

It still doesn't explain why the first event is ignored, but I suspect
it has to do with timing. You're not hooking the click handler to the
button manually, are you? (If you are, maybe you're doing it too late
in the code. Try bumping it up).

Michel.

"Dave Spencer" <sp*******@fuse.net> wrote in message news:<C4***************@fe37.usenetserver.com>...
Hi all,

New to this group and to C# in general (experienced in MFC). Anyway I have
a ListView and a foreach loop that is doing something to each item in the
list and updating the list on the screen. I have a button on the screen
that I'll call STOP that is supposed to stop the processing of the loop in
midstream. I have a bool global variable call bStop. The OnClick for the
stop button sets bStop to true. The loop checks the value of bStop at the
beginning of each loop and calls break if it is true. Problem is bStop is
never set to true until after the loop finishes. In C++ I could make a call
to a function that would use PeekMessage to loop through the message que and
Dispatch any Dialog messages so that the bStop value would be updated.
Don't know how to do this in C#. Any ideas.

Thanks in advance,
Dave

Nov 16 '05 #6
That did it. I think it was just a timing issue as you suggested.

Thanks All
Dave
"fd123456" <fd******@hotmail.com> wrote in message
news:c8**************************@posting.google.c om...
Hi Dave,

Instead of inserting DoEvents() right before you check for the Stop
variable, insert it at the top of your method. DoEvents takes some
processing time and is executed (I believe) on a separate thread,
which means your check is executed concurrently with the beginning of
the events polling.

It still doesn't explain why the first event is ignored, but I suspect
it has to do with timing. You're not hooking the click handler to the
button manually, are you? (If you are, maybe you're doing it too late
in the code. Try bumping it up).

Michel.

"Dave Spencer" <sp*******@fuse.net> wrote in message

news:<C4***************@fe37.usenetserver.com>...
Hi all,

New to this group and to C# in general (experienced in MFC). Anyway I have a ListView and a foreach loop that is doing something to each item in the list and updating the list on the screen. I have a button on the screen that I'll call STOP that is supposed to stop the processing of the loop in midstream. I have a bool global variable call bStop. The OnClick for the stop button sets bStop to true. The loop checks the value of bStop at the beginning of each loop and calls break if it is true. Problem is bStop is never set to true until after the loop finishes. In C++ I could make a call to a function that would use PeekMessage to loop through the message que and Dispatch any Dialog messages so that the bStop value would be updated.
Don't know how to do this in C#. Any ideas.

Thanks in advance,
Dave


Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Randell D. | last post: by
104 posts views Thread by cody | last post: by
1 post views Thread by Dave | last post: by
4 posts views Thread by Vincent | last post: by
3 posts views Thread by gjtired | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.