如何在自己的线程上创建表单并在应用程序整个生命周期中保持打开状态 [英] How to create a form on its own thread and keep it open throughout application lifetime

查看:68
本文介绍了如何在自己的线程上创建表单并在应用程序整个生命周期中保持打开状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小的测试组件,并且遇到了问题

I am creating a little testing component and am running into a problem

基本上,该组件是控制所有对数据库的访问的类上的装饰器,它创建带有两个按钮的表单:模拟丢失的连接"和重新连接".按下按钮,而不是让函数调用通过包装程序,而是抛出NoConnectionException()很好而又简单,并且对测试真正有帮助.

Basically the component is a decorator on a class that controls all access to the database, it creates a form with a two buttons on it: "Simulate Lost Connection" and "Reconnect". Press the button, and instead of letting function calls pass through the wrapper starts throwing NoConnectionException()s nice and simple, and real helpful for testing.

问题在于,此特定应用程序在检测到连接丢失时会出现一个模式对话框连接丢失!".一直坐在那里直到重新建立连接.因为它是模态的,所以我不能按下漂亮的按钮来模拟重新获得的连接.

The problem is that this particular application when it detects a lost connection raises a modal dialog box "connection lost!" that sits there until the connection is regained. Because it is modal I cannot press my nifty button to simulate regained connectivity.

因此,我需要做的是在不同的线程中运行我的小测试表格.我不太确定该怎么做.我尝试过

What I need to do therefore is to run my little testing form in a different thread. I'm not absolutely sure how to do that. I tried

new Thread(
  new ThreadStart(
    (Action)delegate {_form.Start();}
  )
).Start();

但是该方法返回后,线程将立即关闭,因此该表单仅在瞬间显示.

But the thread closes as soon as the method returns so the form never shows up except for an instant.

有什么想法可以实现自己想要的东西吗?

Any idea how I go about achieving what I want?

推荐答案

您将需要在新创建的线程上启动消息循环.您可以通过调用Application.Run(form)来做到这一点.

You will need to start a message loop on the newly created thread. You can do that by calling Application.Run(form).

这篇关于如何在自己的线程上创建表单并在应用程序整个生命周期中保持打开状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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