473,387 Members | 1,624 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,387 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 3199

(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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.