473,468 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 9303
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: 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,...
1
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...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.