C# 有没有可能让一段时间的循环变慢?
重写:我正在编写一个程序,以便更好地理解C语言,并接受用户的输入。现在,我在C#中使用WFA,并在一个文本框中放置我想要粘贴/打印的文本。然后我有另外两个框,它们有间隔输入和数量输入。在我添加了输入量之前,一切都运行得非常好,我已经在用于键入/粘贴文本的计时器上添加了一个while循环。然而,由于某种原因,我不确定。。程序将忽略间隔的任何输入 我基本上是这样设置的C# 有没有可能让一段时间的循环变慢?,c#,C#,重写:我正在编写一个程序,以便更好地理解C语言,并接受用户的输入。现在,我在C#中使用WFA,并在一个文本框中放置我想要粘贴/打印的文本。然后我有另外两个框,它们有间隔输入和数量输入。在我添加了输入量之前,一切都运行得非常好,我已经在用于键入/粘贴文本的计时器上添加了一个while循环。然而,由于某种原因,我不确定。。程序将忽略间隔的任何输入 我基本上是这样设置的 private void timerPaste_Tick(object sender, EventArgs e) {
private void timerPaste_Tick(object sender, EventArgs e)
{
interval = Int32.Parse(interNum.Text);
timerPaste.Interval = interval;
if (typeModebool == true && pasteModebool == false)
{
while (amount > 0) //Need to find a way to make GUI responsive during while loop.
{
SendKeys.Send(textBox1.Text);
SendKeys.Send("{Enter}");
amount--;
}
timerPaste.Enabled = false;
amount = Int32.Parse(amountSetBox.Text);
}
if (pasteModebool == true && typeModebool == false)
{
while (amount > 0) //Need to find a way to make GUI responsive during while loop.
{
Clipboard.SetText(textBox1.Text);
SendKeys.Send("^{c}");
SendKeys.Send("^{v}");
SendKeys.Send("{Enter}");
amount--;
}
timerPaste.Enabled = false;
amount = Int32.Parse(amountSetBox.Text);
}
}
像那样的东西
timerPaste.Elapsed += OnTickEvent;
private async void OnTickEvent(Object source,EventArgs e)
{
interval = Int32.Parse(interNum.Text);
timerPaste.Interval = interval;
if (typeModebool == true && pasteModebool == false)
{
while (amount > 0) //Need to find a way to make GUI responsive during while loop.
{
SendKeys.Send(textBox1.Text);
SendKeys.Send("{Enter}");
amount--;
}
timerPaste.Enabled = false;
amount = Int32.Parse(amountSetBox.Text);
}
if (pasteModebool == true && typeModebool == false)
{
while (amount > 0) //Need to find a way to make GUI responsive during while loop.
{
Clipboard.SetText(textBox1.Text);
SendKeys.Send("^{c}");
SendKeys.Send("^{v}");
SendKeys.Send("{Enter}");
amount--;
}
timerPaste.Enabled = false;
amount = Int32.Parse(amountSetBox.Text);
}
}
事实上,我能够解决这个问题,问题是while循环第一次听到计时器的声音,然后就自己开始响了。我把它换成了if,再加上else语句,现在一切都正常了
int i = 0;
private void timerPaste_Tick(object sender, EventArgs e)
{
if (typeModebool == true && pasteModebool == false) //Check what mode is being used
{
if (i < amount) //If runs until i == amount
{
SendKeys.Send(textBox1.Text);
SendKeys.Send("{Enter}");
amount--;
}
else
{
timerPaste.Enabled = false;
}
}
if (pasteModebool == true && typeModebool == false)
{
if (i < amount)
{
Clipboard.SetText(textBox1.Text);
SendKeys.Send("^{c}");
SendKeys.Send("^{v}");
SendKeys.Send("{Enter}");
amount--;
}
else
{
timerPaste.Enabled = false;
}
}
}
inti=0;
私有void timerPaste_Tick(对象发送方,事件参数e)
{
if(typeModebool==true&&pasteModebool==false)//检查正在使用的模式
{
if(i
您为什么要这样做?这里的代码
段中有什么?系统.线程.线程.睡眠(1000)
?设置一个计时器,伙计…我试着看看我能控制什么,因为我试图在一段时间后回到c。我在发帖之前已经研究过了,否则我就不会有账户了。。我已经尝试将用户输入的两个变量设置为异步,以便在实际循环中延迟。。它不起作用。至于让线程休眠,我已经读到了在处理这段代码时会使应用程序无响应的内容。。这不是我认为理想的东西。我不知道如何使用计时器。如果你有新问题,你应该发布一个新问题。对于timerPaste.appeased+=OnTickEvent。我已经做了一些研究,并尝试根据其他人的帖子将EventArgs更改为ElapsedEventArgs。。它仍然显示出同样的东西