问题描述
大家好!
我当然是这里的新成员,我也是VB.NET的新手.
好吧,我不是英语还是美国人...,所以我不会说英语,对此感到抱歉.
但是在这种情况下,我确实需要帮助.我用Visual Studio 2008创建了一个VB应用程序.它称为随机数".
我要进行该应用程序是因为我有一家女鞋店,所以我想在我的计算机上制作一个程序,以随机价格显示产品.在可以执行该程序之前,该随机数"应用程序是我的第一个小测试.
让我们开始我的问题,嗨.
好吧,这是我的应用程序图形:
< img src ="http://i375.photobucket.com/albums/oo199/congchuatimhoangtu1989/random1.jpg">
如您所见,我的应用程序包含四个主要部分:
-较低的数字(txtLower):这是我输入最低数字的地方.
-较高的数字(txtHigher):这是我放入最高数字的地方.
-按钮随机开始(btRandom):用于激活程序的按钮.
-标签随机(lbRandom):这是较低数字和较高数字之间的随机数字"显示的地方.
我想要一个可以放置最低和最高数字的应用程序,然后程序将自动显示它们之间的随机数.
所以,我让我像这样的代码进行循环测试:
Hi everybody!
I''m a new member here and ofcourse, I''m a newbie of VB.NET, too.
Well, I am not an English or American..., so I am not good at English, sorry about that.
But I really need help in this situation. I created an VB application by Visual Studio 2008. It called "Random Number".
I want to make that application because I have a girl shoes shop, so I want to make a program show Random product with its price in my computer. That "random number" application is my small first test before I can do that program.
Let''s begin my problem, hihi.
Well, this is my application graphic:
<img src="http://i375.photobucket.com/albums/oo199/congchuatimhoangtu1989/random1.jpg">
As you can see, my application has 4 main parts:
- Lower number (txtLower): This is where I put the Lowest number.
- Higher number (txtHigher): This is where I put the Highest number.
- Button Random Begin (btRandom): button to activate the program.
- Lable Ramdom (lbRandom): this is where the "random number" between Lower and Higher number show up.
I want an application where I can put Lowest and Highest number and then the program will show the random number between them automatically.
So, I make I loop test like this code:
Private Sub btRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btRandom.Click
Dim lower, higher, i As Integer
Dim R As New Random
lower = txtLower.Text
higher = txtHigher.Text
For i = 1 To 10
lbRandom.Text = R.Next(lower, higher)
Threading.Thread.Sleep(500)
Next
End Sub
因此,作为我的测试代码,这意味着每经过0.5秒,就会出现一个随机数.
但是,问题是事实并非如此.实际上,它显示等待循环结束. 5秒钟后的平均值(因为我使用1到10的循环,每次500 ms),仅显示1个随机数.
这不是我想要的.
作为SAKryukov,我不应该使用Threading.Thread.Sleep和Appilcation.DoEvent.但是我只是VB的新手,所以如果您不介意,请向我展示其他解决方案的示例.谢谢你.
因此,如果您有时间,能否请您帮助我了解如何解决该问题.
非常感谢!
So, as my test code, it means after each 0,5 second, one random number will show up.
But, the problem is it was not. In fact, It wait for the loop end, it show. Means after 5 seconds (cause i use loop from 1 to 10, each time 500 ms), just 1 random number show.
It is not what I want.
As SAKryukov, I should not use Threading.Thread.Sleep and Appilcation.DoEvent. But I''m just a newbie of VB, so if you don''t mind, please show me an example of another solutions. Thank you.
So, if you have time, can you please help me to know how to solve that problem.
Thank you so much!
推荐答案
namespace RandomTest {
using System.Threading;
internal class ThreadWrapper {
internal class NumberGeneratedEventArgs : System.EventArgs {
internal NumberGeneratedEventArgs(int number) { this.fNumber = number; }
internal int Number { get { return fNumber; } }
int fNumber;
} //class NumberGeneratedEventArgs
internal ThreadWrapper(int sleepTime) {
this.thread = new Thread(this.Body);
this.sleepTime = sleepTime;
} //ThreadWrapper
internal void Start() { this.thread.Start(); }
internal void Abort() { this.thread.Abort(); }
internal void Pause() { this.waitHandle.Reset(); }
internal void Resume() { this.waitHandle.Set(); }
internal event System.EventHandler<NumberGeneratedEventArgs>
NumberGenerated;
void Body() {
int value = 0;
while (true) {
waitHandle.WaitOne();
if (NumberGenerated != null)
NumberGenerated.Invoke(this, new NumberGeneratedEventArgs(value));
value++;
Thread.Sleep(sleepTime);
} //loop
} //Body
Thread thread;
int sleepTime;
ManualResetEvent waitHandle = new ManualResetEvent(false); //non-signalled
} //ThreadWrapper
} //namespace RandomTest
现在,让我们以表格形式使用它.我故意将其全部放在一个文件中,并避免使用Designer,因此所有代码都放在一个位置:
Now, let''s use it in the form. I intentionally made it all in one file and avoided use of Designer, so all code would be in one place:
namespace RandomTest {
using System.Windows.Forms;
public partial class FormMain : Form {
const string ButtonStop = "&Stop";
const string ButtonStart = "&Start";
const int SleepTime = 500;
delegate void NumberAction(Label label, int value);
//in .NET version above 2.0 this line is not needed
//use System.Action<Label, int> instead
public FormMain() {
Padding = new Padding(10);
Button button = new Button();
button.Text = ButtonStart;
button.Dock = DockStyle.Bottom;
Controls.Add(button);
Label output = new Label();
output.Dock = DockStyle.Fill;
output.AutoSize = false;
Controls.Add(output);
button.Click += delegate(object sender, System.EventArgs eventArgs) {
if (running) {
button.Text = ButtonStart;
wrapper.Pause();
} else {
button.Text = ButtonStop;
wrapper.Resume();
} //if
running = !running;
}; //button.click
wrapper.NumberGenerated += delegate(object sender, ThreadWrapper.NumberGeneratedEventArgs eventArgs) {
output.Invoke(new NumberAction(delegate(Label label, int value) {
label.Text = value.ToString();
}), output, eventArgs.Number);
}; //wrapper.NumberGenerated
wrapper.Start();
this.Closing += delegate(object sender, System.ComponentModel.CancelEventArgs e) {
wrapper.Abort();
};
} //FormMain
bool running;
ThreadWrapper wrapper = new ThreadWrapper(SleepTime);
} //class FormMain
} //namespace RandomTest
我还尝试使用与仍在使用的C#v.2兼容的语法.我还希望,如果您仍然想在VB.NET中进行翻译,将会更容易.在更高版本中,匿名委托的lambda语法非常有用.
关于VB.NET,请参阅我对问题的评论.
此代码已经过全面测试.同样,抱歉,这不是VB.NET.
I also tried to use the syntax compatible with C# v.2, which is still used. I also hope it will be easier if you still want to translate it in VB.NET. In later versions, lambda syntax of anonymous delegates is highly beneficial.
As to VB.NET, please see my comment to the question.
This code is fully tested. Again, sorry this is not VB.NET.
这篇关于如何在循环中等待(Vb.net问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!