PDA

Click to See Complete Forum and Search --> : pause my program


gago
Oct 30th, 2007, 10:09 PM
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.

jmcilhinney
Oct 30th, 2007, 10:32 PM
It's really not clear what you mean.

gago
Oct 30th, 2007, 10:52 PM
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.

jmcilhinney
Oct 30th, 2007, 11:03 PM
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.

gago
Oct 30th, 2007, 11:16 PM
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:
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.

jmcilhinney
Oct 30th, 2007, 11:42 PM
Do
If Not Me.dontDoAnything Then
Me.DoSomething()
End If

Thread.Sleep(1000)
LoopYou 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.

jmcilhinney
Oct 30th, 2007, 11:44 PM
Let's try that again in C#:do
{
if (!this.dontDoAnything)
{
this.DoSomething();
}

Thread.Sleep(1000);
} while (true);

gago
Oct 30th, 2007, 11:50 PM
thanks alot !
i'll try.