473,395 Members | 1,689 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,395 software developers and data experts.

button stops loop

Hello

I am just starting to learn c# and am looking for help pls....I want
run a loop until a user clicks a button that stops it...how do I do
this?

Nov 17 '05 #1
8 9299
Hello

You need to run your loop in a separate thread and check something like
NeedToStop flag. If it set, then break the loop. And in UI thread you can
handle button click event to set the flag.

Or you can use ManualResetEvent and check it in your worker thread:
// need to stop ?
if (stopEvent.WaitOne(0, true))
break;
--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"kaiser" <mi*****@cactusblue.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hello

I am just starting to learn c# and am looking for help pls....I want
run a loop until a user clicks a button that stops it...how do I do
this?

Nov 17 '05 #2
Hello

You need to run your loop in a separate thread and check something like
NeedToStop flag. If it set, then break the loop. And in UI thread you can
handle button click event to set the flag.

Or you can use ManualResetEvent and check it in your worker thread:
// need to stop ?
if (stopEvent.WaitOne(0, true))
break;
--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"kaiser" <mi*****@cactusblue.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hello

I am just starting to learn c# and am looking for help pls....I want
run a loop until a user clicks a button that stops it...how do I do
this?

Nov 17 '05 #3
that will be simple , but i accomplished it using worket thread for e.g:

" just copy this code in any form1.cs of a winform application " and
replace the original one completely , i will see if there's a better
method available without using threading .

( Take care of formatting )
********* code *****************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Threading ;
using System.Windows.Forms;
using System.Data;

namespace WindowsTestApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{

private System.Windows.Forms.Button button1;
private static bool loopVariable = true ;
private static string message = "" ;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();

startworkerthread();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 112);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
loopVariable = false ;
}

private void startworkerthread()
{
Thread tt = new Thread(new ThreadStart(loopMethod)) ;

tt.Start() ;
}

private void loopMethod()
{
while(loopVariable)
{
message += "Something" ;
}

MessageBox.Show("stopped " + message) ;
}
}
}
- Mrinal

kaiser wrote:
Hello

I am just starting to learn c# and am looking for help pls....I want
run a loop until a user clicks a button that stops it...how do I do
this?

Nov 17 '05 #4
that will be simple , but i accomplished it using worket thread for e.g:

" just copy this code in any form1.cs of a winform application " and
replace the original one completely , i will see if there's a better
method available without using threading .

( Take care of formatting )
********* code *****************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Threading ;
using System.Windows.Forms;
using System.Data;

namespace WindowsTestApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{

private System.Windows.Forms.Button button1;
private static bool loopVariable = true ;
private static string message = "" ;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();

startworkerthread();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 112);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
loopVariable = false ;
}

private void startworkerthread()
{
Thread tt = new Thread(new ThreadStart(loopMethod)) ;

tt.Start() ;
}

private void loopMethod()
{
while(loopVariable)
{
message += "Something" ;
}

MessageBox.Show("stopped " + message) ;
}
}
}
- Mrinal

kaiser wrote:
Hello

I am just starting to learn c# and am looking for help pls....I want
run a loop until a user clicks a button that stops it...how do I do
this?

Nov 17 '05 #5
Thanks....that soln a bit beyond me at the mo but I will start to work
through it....the loop doesnt seem to start?

Nov 17 '05 #6
Thanks....that soln a bit beyond me at the mo but I will start to work
through it....the loop doesnt seem to start?

Nov 17 '05 #7
Sorry...I see it does run. thanks

Nov 17 '05 #8
Sorry...I see it does run. thanks

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rick | last post by:
After being frustrated with this issue on several occasions I think I found the secret sauce for solving the issue after reading a few different messages about what others thought and trying a...
0
by: kaiser | last post by:
Hello I am just starting to learn c# and am looking for help pls....I want run a loop until a user clicks a button that stops it...how do I do this?
9
by: mareal | last post by:
I have noticed how the thread I created just stops running. I have added several exceptions to the thread System.Threading.SynchronizationLockException System.Threading.ThreadAbortException...
14
by: dawnerd | last post by:
Hi, I am developing a CMS and came across something which has never happened to me before, and I re-wrote the specific script twice, both differently, and still had the same error. I'm not sure...
1
by: =?Utf-8?B?UmljaA==?= | last post by:
In a database search application (vb2005), the user wants to be able to scroll through records using the mousewheel. The data display form contains textboxes for the main data and a datagridview...
8
by: mustakbal | last post by:
This has to be very simple but I can't get it right and I'm getting frustrated with it. My code for the Next button works fine while using "index1" for lowerlimit and "index2" for the upperlimit and...
2
by: thomas.lidebrandt | last post by:
Hello I'm using wxPython to consruct a GUI and want to change a button label and event handler to change after the button have been pressed. The only thing I can think of is a global variable...
13
by: Sunbags | last post by:
Hello, I'm a 2nd year Computer Engineering student and I have a problem with my VB6 code. I've just started learning VB6 for a project in which we have to create a form which displays the...
1
by: Jon Todd | last post by:
Happy holidays all! I'm developing an application and, as a relative newbie to Python, have come across a stumbling block with Tkinter. I'd like to have a button that when pressed executes a...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.