首页 \ 问答 \ 只使用一次定时器(Using Timer only once)

只使用一次定时器(Using Timer only once)

我想在主窗体初始化1秒后只使用一次定时器。 我认为以下内容会有一个消息框只说一次“Hello World”,但实际上每隔一秒新消息框就会显示“Hello World”。

为什么这样? 我在tick事件中放置了t.Stop() 。 另外,我是否需要以某种方式处理计时器以避免内存泄漏?

        Timer t = new Timer();
        t.Interval = 1000;                
        t.Tick += delegate(System.Object o, System.EventArgs e)
                        { MessageBox.Show("Hello World"); t.Stop(); };

        t.Start();   

请帮助并显示是否有更好的方法来做到这一点? 谢谢。


I want to use a timer only once, at 1 second after the initialization of my main form. I thought the following would have a message box saying "Hello World" just once, but actually a new message box says "Hello World" every one second.

Why so? I had put t.Stop() in the tick event. Also, do I need to dispose the timer somehow to avoid memory leakage?

        Timer t = new Timer();
        t.Interval = 1000;                
        t.Tick += delegate(System.Object o, System.EventArgs e)
                        { MessageBox.Show("Hello World"); t.Stop(); };

        t.Start();   

Please help and show if there is a better way of doing this? Thanks.


原文:https://stackoverflow.com/questions/2841177
更新时间:2022-04-15 17:04

最满意答案

1. 你的图呢?

2. 交互界面和直接运行py文件是不一样的,主要有两点,一个是后者如果不是显示调用print之类的函数,是没有屏幕输出的,另一个是后者如果所有代码运行完了,程序会直接退出,而前者还是待在交互界面
3. 如果要不退出,可以在py文件最后加一行

input()

其他回答

及贫困县寻啡合同法曝

相关问答

更多
  • E:\Python27\python -m idlelib -n 用上面的命令试试看 idle具体怎么打不开,有何提示
  • 以前也这么干过。其实pyton3基本上没有用。可以不用学,学好了python2,以后换过去也就是1天的事情。注意一些编程习惯与规范尽量向python3靠就可以了。 另外virtualenv这个工具,可以让你安装多套python,不相互冲突。 不过既然你安装了,那么就可以解决,先要把电脑属性,高级里的系统的环境变量path里做修改,去掉python3的路径,加上python2的。因为两者不能相互使用。只能保留一个。 另一个地方比较的麻烦。是修改注册表的一个位置。需要将里面注册的python3改成python2 ...
  • 这个是PyScripter,是Win上的一个Python集成环境,直接下载的Python中是不会带的,要自己单独下,但是Idle的功能也够了。
  • 1. 你的图呢? 2. 交互界面和直接运行py文件是不一样的,主要有两点,一个是后者如果不是显示调用print之类的函数,是没有屏幕输出的,另一个是后者如果所有代码运行完了,程序会直接退出,而前者还是待在交互界面 3. 如果要不退出,可以在py文件最后加一行 input()
  • 由于您已经安装了Anaconda,因此您可以通过在终端中执行以下命令来创建Python虚拟环境 - conda create -n pythonenvname python=x.x anaconda 由于您正在尝试创建Python 2.7虚拟环境,为了方便起见,我建议您使用python27作为您的环境名称。 而且, xx指的是将用于创建环境的Python版本。 在你的情况下,它是2.7 。 所以这是您应该运行以设置您的虚拟环境的命令 - conda create -n python27 python=2. ...
  • 您在OSX上的系统Python和您安装的Python之间发生了Python版本冲突。 这可以通过多种方式解决,但我强烈建议您通过Homebrew重新安装Python。 使用Homebrew安装Python时,也会安装pip和setup_tools ,所有内容都将被处理(您不需要使用sudo来安装Python模块)。 请参阅: https : //github.com/Homebrew/homebrew/wiki/Homebrew-and-Python 使用Homebrew管理Python发行版可以省去很多麻 ...
  • 是的,这段代码应该放在wsgi.py文件中。 举个例子(尽管这个例子使用的是3.3版本,它会指向正确的方向),请查看提供的答案下的注释: 在OpenShift的书籍示例中使用Python 3.3 Yes, this code should go in the wsgi.py file. For an example (although the example uses version 3.3, it'll point you in the right direction), look at the comm ...
  • 要将其用作CGI,您必须将脚本移动到cgi-bin或HTTP服务器的类似目录中。 然后将浏览器指向http://127.0.0.1/cgi-bin/my_scipt.py并查看结果。 如果出现问题,请参阅HTTP服务器错误日志。 如果出现奇怪错误,请告诉我们您使用的HTTP服务器和操作系统,例如“WinXP上的Apache 2.2”。 To use it as CGI you must move your script into cgi-bin or similar directory of HTTP se ...
  • 原因是你已经安装了python 3的烧瓶,但是没有安装python 2.7我假设你可能已经使用pip3来安装烧瓶,如果是这种情况, pip3会为python3安装,而pip会默认为python 2.7安装。 因此,如果安装在python 3上,请检查安装在哪里,如果安装在python 2.7上,它应该在/ usr / lib / python3 / dist-packages下,它应该位于/usr/lib/python2.7/dist-packages下。 可能还有其他的lib路径,其中包也将被搜索。 查看 ...
  • 尝试在print(post2.text)后插入这些行print(post2.text) A=post2.text A=A.encode('ascii','ignore') 而不是text_file.write(post2.text + "\n")写入text_file.write(A + "\n") 它为我工作。 要调试下一个按钮问题,请尝试以下代码: for n in range(10): nextbutton = 0 try: nextbutton = driver.find_element ...

相关文章

更多

最新问答

更多
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)
  • 是否可以嵌套hazelcast IMaps?(Is it possible to nest hazelcast IMaps? And whick side effects can I expect? Is it a good Idea anyway?)
  • UIViewAnimationOptionRepeat在两个动画之间暂停(UIViewAnimationOptionRepeat pausing in between two animations)
  • 在x-kendo-template中使用Razor查询(Using Razor query within x-kendo-template)
  • 在BeautifulSoup中替换文本而不转义(Replace text without escaping in BeautifulSoup)
  • 如何在存根或模拟不存在的方法时配置Rspec以引发错误?(How can I configure Rspec to raise error when stubbing or mocking non-existing methods?)
  • asp用javascript(asp with javascript)
  • “%()s”在sql查询中的含义是什么?(What does “%()s” means in sql query?)
  • 如何为其编辑的内容提供自定义UITableViewCell上下文?(How to give a custom UITableViewCell context of what it is editing?)
  • c ++十进制到二进制,然后使用操作,然后回到十进制(c++ Decimal to binary, then use operation, then back to decimal)
  • 以编程方式创建视频?(Create videos programmatically?)
  • 无法在BeautifulSoup中正确解析数据(Unable to parse data correctly in BeautifulSoup)
  • webform和mvc的区别 知乎
  • 如何使用wadl2java生成REST服务模板,其中POST / PUT方法具有参数?(How do you generate REST service template with wadl2java where POST/PUT methods have parameters?)
  • 我无法理解我的travis构建有什么问题(I am having trouble understanding what is wrong with my travis build)
  • iOS9 Scope Bar出现在Search Bar后面或旁边(iOS9 Scope Bar appears either behind or beside Search Bar)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • 有关调用远程WCF服务的超时问题(Timeout Question about Invoking a Remote WCF Service)