|
-
Oct 30th, 2007, 10:09 PM
#1
Thread Starter
New Member
pause my program
hi! i have a problem with my program:
i am making a program that can simulate many algorithms
so,i use thread to delay when program is running.
but i don't know how to pause when an algorithm is running.
guys! who can help me to solve this problem.
thanks.
-
Oct 30th, 2007, 10:32 PM
#2
Re: pause my program
It's really not clear what you mean.
-
Oct 30th, 2007, 10:52 PM
#3
Thread Starter
New Member
Re: pause my program
i used:
System.Threading.Thread.Sleep(timedelay); //timedelay :int
then i merge this code into algorithm.
so i want my algorithm will ran step by step when an user click to a button in my form.
If it is a timer ,i can use enabled property to pause by this way:
timer1.Enabled = !timer1.Enabled;
but Thread ,have no way.
-
Oct 30th, 2007, 11:03 PM
#4
Re: pause my program
Disabling a Timer doesn't pause anything. If a Timer is enabled then it will Tick and at the Tick event you can then make something happen. If the Timer is not Enabled then it doesn't Tick so it never invokes any functionality. That's just like asking how you stop yourself walking. You don't have to do anything to stop yourself walking; you just don't take a step. It's the absence of doing something.
A thread is something completely different to a Timer, so of course interacting with it is completely different. If you want to pause a thread then you call Thread.Sleep, as you've shown. What else would there be? Why would you need something else? I'm still confused, or else you are.
-
Oct 30th, 2007, 11:16 PM
#5
Thread Starter
New Member
Re: pause my program
why not ?
such as:
in my form i have 3 controls:
a timer:timer1 timer1.Interval = 100;
a label to display value of an integer:labelTest
a button:button1
then my code:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ExamineTimer
{
public partial class Form1 : Form
{
int count = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = !timer1.Enabled;
}
private void timer1_Tick(object sender, EventArgs e)
{
count++;
labelTest.Text = count.ToString();
}
}
}
i want to manipulate with Thread similar to Timer
i want to pause a thread then i call Thread.Sleep
Thanks.
Last edited by gago; Oct 30th, 2007 at 11:24 PM.
-
Oct 30th, 2007, 11:42 PM
#6
Re: pause my program
vb.net Code:
Do
If Not Me.dontDoAnything Then
Me.DoSomething()
End If
Thread.Sleep(1000)
Loop
You can set the dontDoAnything variable to True or False wherever you like. If it's set to True then the action will be performed with a second between each execution. If it's set to False nothing will happen and the value will just be checked every second.
-
Oct 30th, 2007, 11:44 PM
#7
Re: pause my program
Let's try that again in C#:
csharp Code:
do
{
if (!this.dontDoAnything)
{
this.DoSomething();
}
Thread.Sleep(1000);
} while (true);
-
Oct 30th, 2007, 11:50 PM
#8
Thread Starter
New Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|