Click here to Skip to main content
15,790,917 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
so here is the problem i have developed a program that copies the text entered in one textbox to another one(like a chatbox)...........but the problem is that I want to write a group of if conditions on which the program will respond but the problem is that i have created delay like using system.threading.thread.sleep(1000)so here comes the problem.......the program imediately goes to the matching condiition without executing anything else before so the text also gets copied after one second.........and so does response.........but what I want is to copy the text first and response later.........Thanks in advance....

Ahsan Naveed.

1 solution

Don't use thread.sleep - particularly not in your normal (or UI thread). It stops the whole thread from doing anything, and thus the system from responding to the user inputs...

Probably what you want to do is use a Timer and do your conditional processing in the Tick event - but without seeing your code, it's not possible to be any more precise.

"i hope i make my point cause i don't have a better way to explain it."

I must admit - that code doesn't make a whole lot of sense! :laugh:
Ignoring that it won't compile as a code fragment - there is a close curly bracket above the "continue" label that isn't matched in the code fragment, so I don't know how relevant it is.

The first thing to do is remove that label, and then remove the word "goto" from your memory. As a beginner, if you have to use "goto" and labels in your code, then you have done something very, very wrong! I haven't used it once since I learned C#, simply because I haven't needed it. You shouldn't either. ;)

I am assuming that what you are trying to do is put a delay between the first bit and the second?
OK - lets do it. Firstly, let's tidy up your code a little. Replace the code above the "continue" with this:
if (textBox1.Text != "")
    textBox2.Text += textBox1.Text + "\r\n";
Visually to the user it does the same thing as your code.
Then, let's do something really silly...let's throw your code away...:laugh:
Instead, let's create a couple of class level variables:
private int waitTimer = 0;
private string waitMessage = "";

and then let's tidy up your other code and use these instead.
if (textBox1.Text.ToLower() == "what are you?")
    waitMessage = "I am a computerized robot and my name is Dr. A";
    waitTimer = 4;
What this does is pretty obvious, except for the first part - which converts all the uppercase characters in the textbox to lower case and then checks them. This means that you don't have to type the string loads of times, and get it wrong occasionally, and the user can type it all in uppercase if he is feeling angry!
But that does nothing to the display - so use the Timer code I gave you before, and put this in the Tick event handler:
if (waitTimer > 0)
    if (--waitTimer == 0)
        textBox2.Text += waitMessage + "\r\n";

The value "4" in the waitTimer is supposed to match with the "250" I put in the Interval property of the Timer, to give about one second. Larger numbers will make the delay longer, smaller will make it shorter.

Now try it!
Share this answer
Ahsan98 25-Aug-13 6:33am    
I also know that timers are better but alas I dont know how to use them....i am a beginner and dont have the knowledge on the usage of timers..........i dont know what to do i found this method so i used it please help..........
OriginalGriff 25-Aug-13 6:44am    
That's strange - you seemed to understand when I explained yesterday:

What part of it is giving you problems?
Ahsan98 26-Aug-13 7:07am    
yes i did but the problem here is that i have to make a program that answer to what you say.......and it uses a lot of it statements and the way you used the timer showed that you have to write what to do in a timer so that means i have to create separate timer for every condition and thats a great bunch of problem also far as i think it is also gonna create the same problem as c# directly goes to the condition which matches the text so its still gonna do write my text s late.....please just tell me some method on how to MAKE some bunch of code executed in the end...........
OriginalGriff 26-Aug-13 7:33am    
First off, calm down, and use punctuation...:laugh:
Why would you have to create a separate timer for each condition? I don't. I generally set up a single time, with an interval set to about half of the minimum time interval I'm going to be interested in, and then have individual counters for each of the "actions" that need some kind of timing. Then, if I have a lot of "actions" I'll move each one into a separate method and call them in sequence from the timer Tick handler.

So what do you mean by "as c# directly goes to the condition which matches the text" because that doesn't make a lot of sense...
Ahsan98 26-Aug-13 8:03am    
ok so here's the code for better explanation
string a;
a = textBox1.Text;if (textBox2.Text == "")
textBox2.Text = a;
else if (textBox1.Text == "")
textBox2.Text += "\r\n" + a;
if (textBox1.Text == "What are you?" || textBox1.Text == "what are you?")
textBox2.Text += "\r\nI am a computerized robot and my name is Dr. A";

so the problem is that when i write what are you? it directly goes to line referenced 'condition'...and so if i use timer sleep my text also gets copied to the textbox2 after one second.......and the way you used the timer it would give only one answer so for every condition i develop a seperate timer as far as i get it...... :) although using the timer is still going to create the same problem(according to me)... :)

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