473,788 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3214

(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.us enetserver.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.us enetserver.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.DoE vents() in your foreach loop?

Michel

"Dave Spencer" <sp*******@fuse .net> wrote in message news:<C4******* ********@fe37.u senetserver.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******@hotma il.com> wrote in message
news:c8******** *************** ***@posting.goo gle.com...
Hi Dave,

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

Michel

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

news:<C4******* ********@fe37.u senetserver.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.u senetserver.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******@hotma il.com> wrote in message
news:c8******** *************** ***@posting.goo gle.com...
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.u senetserver.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
4302
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 code everytime I found more than one method to perform a single task. I timed each method to find which would complete faster. I thought I'd share my most recent results which (I believe) should help those write their programs to be more...
104
7202
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 foeach(Color c in Red..Blue) { } // using enums It should work with all integral datatypes. Maybe we can step a bit further: foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred
15
2678
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 "foreach" loop instead of a "for" loop. This made absolutely no sense to me, but the author had posted his code and timing results. The "foreach" (a VB and other languages construct) was 0.01 seconds to access 1000 lines in rich text box,...
1
1271
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, System.EventArgs e) { int a= 1; int b= 0; while (a != 0) {
4
1606
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 dataRow which then should be removed, through a foreach loop and then the loop is being exited by break statement. The problem is that for some reason, my (custom control) button is being fired again, which may trigger an Exception since there might...
13
14499
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 findListBox.listBox2.Items) { listBox2.Items count changed, restart this foreach } Thanks for any help.
3
2507
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
33371
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 code is here: foreach ( string propertyName in ht.Keys ) { this.setProperty( propertyName, ht );
5
13153
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 collection, as if they were an array of radio buttons ? Basically i want to plonk the radio buttons on a panel then use a for loop to index through the radio buttons with an idenex to check thier state. I want to do it this way becasue i want to...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.