Randomize from array
-
Hi all, I got small trobule with randomize her my code for starting randomize private void lblStart_Click(object sender, EventArgs e) { bool run; run = true; Random d = new Random(); Application.DoEvents(); while (run == true) { Application.DoEvents(); int rnd = d.Next(0, lst.Count - 1); lblNumber.Text = lst[rnd].ToString(); } } here for stop randomize and show result in messagebox and Label private void lblStop_Click(object sender, EventArgs e) { run = false; MessageBox.Show(lblNumber.Text); } after I click stop button in label and messagebox it show the same text, but after I clcik OK button in label show another number? Can anyone help to correct this? Thanks
Socheat
-
Hi all, I got small trobule with randomize her my code for starting randomize private void lblStart_Click(object sender, EventArgs e) { bool run; run = true; Random d = new Random(); Application.DoEvents(); while (run == true) { Application.DoEvents(); int rnd = d.Next(0, lst.Count - 1); lblNumber.Text = lst[rnd].ToString(); } } here for stop randomize and show result in messagebox and Label private void lblStop_Click(object sender, EventArgs e) { run = false; MessageBox.Show(lblNumber.Text); } after I click stop button in label and messagebox it show the same text, but after I clcik OK button in label show another number? Can anyone help to correct this? Thanks
Socheat
All you are doing there is clogging up your processor, to no good end. Do it this way: Create a class level Timer, call it randomTimer. Move your Random to class level. In your Start_Click event handler, check if there is an existing Timer in randomTimer. If not, create an instance, set the interval to 1/10th second, and set a handler for the Tick event. In all cases, start the timer. In the Tick event, set the lblNumber.Text to your random value. In your Stop_Click event handler, stop the timer.
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
All you are doing there is clogging up your processor, to no good end. Do it this way: Create a class level Timer, call it randomTimer. Move your Random to class level. In your Start_Click event handler, check if there is an existing Timer in randomTimer. If not, create an instance, set the interval to 1/10th second, and set a handler for the Tick event. In all cases, start the timer. In the Tick event, set the lblNumber.Text to your random value. In your Stop_Click event handler, stop the timer.
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
Hi, Can you write me example? Thanks
Socheat
-
All you are doing there is clogging up your processor, to no good end. Do it this way: Create a class level Timer, call it randomTimer. Move your Random to class level. In your Start_Click event handler, check if there is an existing Timer in randomTimer. If not, create an instance, set the interval to 1/10th second, and set a handler for the Tick event. In all cases, start the timer. In the Tick event, set the lblNumber.Text to your random value. In your Stop_Click event handler, stop the timer.
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
Here code I just used timer control but still can't use bool run = true; private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = true; this.Text = System.DateTime.Now.ToString(); run = true; Random rnd = new Random(); int i = 0; while (run == true) { Application.DoEvents(); i = rnd.Next(0, 1000); textBox1.Text = i.ToString(); } } private void button1_Click(object sender, EventArgs e) { timer1_Tick(sender, e); } private void button2_Click(object sender, EventArgs e) { timer1.Enabled = false; run = false; MessageBox.Show(textBox1.Text); }
Socheat
-
Here code I just used timer control but still can't use bool run = true; private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = true; this.Text = System.DateTime.Now.ToString(); run = true; Random rnd = new Random(); int i = 0; while (run == true) { Application.DoEvents(); i = rnd.Next(0, 1000); textBox1.Text = i.ToString(); } } private void button1_Click(object sender, EventArgs e) { timer1_Tick(sender, e); } private void button2_Click(object sender, EventArgs e) { timer1.Enabled = false; run = false; MessageBox.Show(textBox1.Text); }
Socheat
No. Read up on Timer.
private Timer timer = new Timer(); private void buttonStart\_Click(object sender, EventArgs e) { timer.Interval = 1000 / 100; // Ten times a second timer.Tick += new EventHandler(timer\_Tick); timer.Start(); } void timer\_Tick(object sender, EventArgs e) { ... Do a single instance of your Random stuff. } private void buttonStop\_Click(object sender, EventArgs e) { timer.Stop(); }
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
Here code I just used timer control but still can't use bool run = true; private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = true; this.Text = System.DateTime.Now.ToString(); run = true; Random rnd = new Random(); int i = 0; while (run == true) { Application.DoEvents(); i = rnd.Next(0, 1000); textBox1.Text = i.ToString(); } } private void button1_Click(object sender, EventArgs e) { timer1_Tick(sender, e); } private void button2_Click(object sender, EventArgs e) { timer1.Enabled = false; run = false; MessageBox.Show(textBox1.Text); }
Socheat
BTW: You don't need to send me an email as well as replying - the reply automatically sends an email. Just like this one, in fact...
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
Hi all, I got small trobule with randomize her my code for starting randomize private void lblStart_Click(object sender, EventArgs e) { bool run; run = true; Random d = new Random(); Application.DoEvents(); while (run == true) { Application.DoEvents(); int rnd = d.Next(0, lst.Count - 1); lblNumber.Text = lst[rnd].ToString(); } } here for stop randomize and show result in messagebox and Label private void lblStop_Click(object sender, EventArgs e) { run = false; MessageBox.Show(lblNumber.Text); } after I click stop button in label and messagebox it show the same text, but after I clcik OK button in label show another number? Can anyone help to correct this? Thanks
Socheat
Hi, your code contains a couple of mistakes against general rules, including: 1. event handlers (such as a Click handler) should execute and be done with in less than say 20 milliseconds; anything that takes longer needs a different approach, maybe a timer (a System.Windows.Forms.Timer is best for GUI related stuff), maybe a separate thread; 2. don't abuse Application.DoEvents(), there are a few situations where its use would be OK, yours isn't one of them. 3. avoid "busy loops", such as your while(run) construct, whenever there is an elegant alternative. Obeying (1) often makes (2) moot. Griff's approach is fine. PS: please use PRE tags when including code snippets. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
Hi, your code contains a couple of mistakes against general rules, including: 1. event handlers (such as a Click handler) should execute and be done with in less than say 20 milliseconds; anything that takes longer needs a different approach, maybe a timer (a System.Windows.Forms.Timer is best for GUI related stuff), maybe a separate thread; 2. don't abuse Application.DoEvents(), there are a few situations where its use would be OK, yours isn't one of them. 3. avoid "busy loops", such as your while(run) construct, whenever there is an elegant alternative. Obeying (1) often makes (2) moot. Griff's approach is fine. PS: please use PRE tags when including code snippets. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
I allow myself to use DoEvents() inside an event handler when I am sure there is no risk of the event handler being re-entered by doing so, and I want the GUI to settle before I continue. Case in point would be a MenuItem.Click handler on say an "Open File..." menu item, where I want the menu to disappear and the GUI restored ASAP before opening the dialog that was ordered. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
I allow myself to use DoEvents() inside an event handler when I am sure there is no risk of the event handler being re-entered by doing so, and I want the GUI to settle before I continue. Case in point would be a MenuItem.Click handler on say an "Open File..." menu item, where I want the menu to disappear and the GUI restored ASAP before opening the dialog that was ordered. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
Hi all, I got small trobule with randomize her my code for starting randomize private void lblStart_Click(object sender, EventArgs e) { bool run; run = true; Random d = new Random(); Application.DoEvents(); while (run == true) { Application.DoEvents(); int rnd = d.Next(0, lst.Count - 1); lblNumber.Text = lst[rnd].ToString(); } } here for stop randomize and show result in messagebox and Label private void lblStop_Click(object sender, EventArgs e) { run = false; MessageBox.Show(lblNumber.Text); } after I click stop button in label and messagebox it show the same text, but after I clcik OK button in label show another number? Can anyone help to correct this? Thanks
Socheat
Hi all, I can do it now. I no need to remove Application.DoEvent(); otherwise my application will hang. I just remove my while loop and add it in timer class, it OK now. Thanks.
Socheat
-
Hi all, I can do it now. I no need to remove Application.DoEvent(); otherwise my application will hang. I just remove my while loop and add it in timer class, it OK now. Thanks.
Socheat
If you have removed your while loop, and put the code into the timer, then you do not need Application.DoEvents(). If your app hangs without it, then you are still doing something very wrong!
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
Hi all, I got small trobule with randomize her my code for starting randomize private void lblStart_Click(object sender, EventArgs e) { bool run; run = true; Random d = new Random(); Application.DoEvents(); while (run == true) { Application.DoEvents(); int rnd = d.Next(0, lst.Count - 1); lblNumber.Text = lst[rnd].ToString(); } } here for stop randomize and show result in messagebox and Label private void lblStop_Click(object sender, EventArgs e) { run = false; MessageBox.Show(lblNumber.Text); } after I click stop button in label and messagebox it show the same text, but after I clcik OK button in label show another number? Can anyone help to correct this? Thanks
Socheat