如何在其他代码之后执行条件? [英] How to execute a condition after some other code?

查看:55
本文介绍了如何在其他代码之后执行条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以问题是我开发了一个程序,将在一个文本框中输入的文本复制到另一个文本框(如聊天框)...........但问题是我想写一个如果程序将响应的条件,但问题是我已经创建了延迟,如使用system.threading.thread.sleep(1000)所以这里出现问题.......程序立即进入匹配没有执行任何其他事情的条件,所以文本也会在一秒后被复制.........响应也是如此.........但我想要的是先复制文本然后再回复.........提前致谢....







问候,

Ahsan Naveed。

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....



Regards,
Ahsan Naveed.

推荐答案

不要使用thread.sleep - 尤其不要用于普通(或UI线程)。它阻止整个线程做任何事情,因此系统响应用户输入...



你想要做的就是使用Timer并做你在Tick事件中的条件处理 - 但是没有看到你的代码,就不可能更准确了。





我希望我能指出我的观点,因为我没有更好的方法来解释它。



我必须承认 - 代码没有很有意义! :笑:

忽略它不会编译成代码片段 - 在continue标签上面有一个紧密的大括号,在代码片段中没有匹配,所以我不知道知道它有多相关。



要做的第一件事就是删除那个标签,然后从你的记忆中删除goto这个词。作为一个初学者,如果你必须在代码中使用goto和标签,那么你做了非常非常错误的事情!自从我学习C#以来,我没有使用过它,只是因为我不需要它。你不应该。 ;)



我假设你要做的是在第一位和第二位之间放一个延迟?

OK - 我们开始做吧。首先,让我们整理一下你的代码。用以下代码替换continue上方的代码:

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;
    }
textBox1.Clear();

这一点非常明显,除了第一部分 - 它将文本框中的所有大写字符转换为小写然后检查他们。这意味着您不必键入字符串加载次数,偶尔会出错,如果用户生气,用户可以用大写字母输入!

但这对显示没有任何作用 - 所以请使用我之前给你的Timer代码,并把它放在Tick事件处理程序中:

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";
        }
    }



waitTimer中的值4应该与我放入的Interval属性中的250匹配定时器,给大约一秒钟。较大的数字会使延迟更长,较小的数字会使延迟更短。



现在尝试一下!


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!


这篇关于如何在其他代码之后执行条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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