473,231 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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 3193

(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
1
by: Dave | last post by:
How would I program an interupt. If I have two buttons on a form, one starts a loop and the other stops the loop. What code would I need to add? private void button1_Click(object sender,...
4
by: Vincent | last post by:
Hello all, I have an application that has a ListView and a DataSet. When btnRemove is clicked, the method btnRemove_Click is being fired, which compares 2 keys to find a unique match on a...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
3
by: gjtired | last post by:
Hi, When I click a button or dropdown box at the bottom of the web form the focus shifts to the top of the form. What can I do to make it stay where it is at? Thanks Gayle
3
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
5
by: Peted | last post by:
I know you can iterate through a collection of radio buttons in a panel, using a "for each in control" type iteration that c# supports, but is it possible to iterate through the radio buttons...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.