定时器控制等待时间 [英] Timer control waiting time

查看:138
本文介绍了定时器控制等待时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在文本框中显示值,但是循环变得更快并且存在。

我给了等待时间。但它没有回复。



Iam Trying to Show values in Text Box, But Loops gets faster and exists out.
I have given Wait Time. But It doesn't respond.

for (int i = j; i <= ds.Tables[0].Rows.Count; i++)
           {
                   string Sname = ds.Tables[0].Rows[i][SB].ToString();
                   txt_TextBox1.Text = Sname.ToString();
                   txt_TextBox1.Focus();
              // Delay Time Which I have taken from Input User- 8000 MilliSeconds
               Task.Delay(Convert.ToInt32(Stimer));
             }





我尝试过:



我把文本框集中了。但它从循环中快速存在。



What I have tried:

I have made Text box focused. But it exists fast from the loop.

推荐答案

按照这个例子



Follow this example

async Task MyDelay(int time)
       {
           await Task.Delay(time);
       }

       private async void button1_Click(object sender, EventArgs e)
       {
           int delayinMilliSec = 1000; // 1 second delay
           for (int i = 0; i < 10; i++)
           {
               txt_TextBox1.Text = i.ToString();
               txt_TextBox1.Focus();
               await MyDelay(delayinMilliSec);
           }

       }





来自 Thread.Sleep与Task.Delay [ ^ ]



在您的密码中



referred from Thread.Sleep vs. Task.Delay [^]

In your code

for (int i = j; i <= ds.Tables[0].Rows.Count; i++)





< = 导致索引异常,



<= results in index exception,

string Sname = ds.Tables[0].Rows[i][SB].ToString();



始终使用


always use


Sname 之后调用 ToString()因为你已经有一个字符串,所以没用。



将焦点放在一个没有多大意义在循环中控制。



根据.NET约定,您的变量未正确命名。变量应以小写字母开头。



您的变量名称不具描述性。



通常,它最好使用 int.TryParse Int32.TryParse方法(系统) [ ^ ])将字符串转换为整数。



由于应用程序将停止响应,因此在UI线程内直接延迟没有多大意义。阅读文档是个好主意: Task.Delay方法(TimeSpan) )(System.Threading.Tasks) [ ^ ]。您应该使用 await 关键字,如示例中所示,不会阻止UI线程。



还要检查带整数的版本,因为它提供了有关使用 await 的一些额外信息: Task.Delay Method(Int32)(System.Threading.Tasks) [ ^ ]。



另请参阅该代码: TechNet Wiki [ ^ ]



一些额外链接:

不要使用async和aw死锁ait [ ^ ]

会阻止吗?揭穿异步/等待陷阱 - 代码之旅 [ ^ ]
The call to ToString() after Sname is completly useless as you already have a string.

It does not make much sense to give the focus to a control inside a loop.

Your variables are not properly named according to .NET convention. Variables should start with a lowercase letter.

Your variable name are not descriptive.

Usually, it is better to use int.TryParse (Int32.TryParse Method (System)[^]) to convert a string to an integer.

It does not make much sense to directly have a delay inside UI thread as the application will stop responding. It would be a good idea to read the documentation: Task.Delay Method (TimeSpan) (System.Threading.Tasks)[^]. You should probably use await keyword as in the example so that the UI thread won't be blocked.

Also check the version that take an integer as it give some extra information about the use of await: Task.Delay Method (Int32) (System.Threading.Tasks)[^].

See also that code: TechNet Wiki[^]

Some extra links:
Don't deadlock with async and await[^]
Will it block? Debunking async/await pitfalls - Journey of Code[^]


试试这样:



Try it like this :

private int cnt = 0;

private void TimerMethod()
{
    string sname = ds.Tables[0].Rows[i][SB].ToString();
    txt_TextBox1.Text = sname;

    cnt += 1;
    if (cnt > ds.Tables[0].Rows.Count) { cnt = 0; }
}





...并通过Timer.Tick调用此Timer-Method(分配此事件的方法)

另外你应该设置Timer-Intervall内部实现的延迟时间(定时器的属性)





附加:

如果你想激活你的TextBox,你应该使用TextBox.Select而不是TextBox.Focus。



... and call this Timer-Method by the Timer.Tick (assign the method to this Event)
Also you should set the delay-Time for the actualisation inside the Timer-Intervall (the Property of the Timer)


Additional:
If you want to have your TextBox active you should use TextBox.Select instead of TextBox.Focus.


这篇关于定时器控制等待时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆