Click here to Skip to main content
15,907,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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.
Posted
Updated 19-Aug-16 1:00am
Comments
Andy Lanng 18-Aug-16 10:27am    
Why are you setting the delay in a loop. It doesn't matter how many times you set it to [8000], it'll still be 8000 from the start, not from when you set it.
On reflection, I may be wrong

Follow this example

C#
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);
           }

       }


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

In your code
C#
for (int i = j; i <= ds.Tables[0].Rows.Count; i++)


<= results in index exception,
C#
string Sname = ds.Tables[0].Rows[i][SB].ToString();

always use
 
Share this answer
 
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[^]
 
Share this answer
 
Try it like this :

C#
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; }
}


... 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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900