在单独的线程中打开新表格 [英] Opening new form in seperate thread Threads

查看:68
本文介绍了在单独的线程中打开新表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了Form1.在Form1中,我们有一个菜单栏.通过在菜单栏上单击,我们必须打开另一个窗体,例如Form2和Form1应该处于停用状态,而Form1仍处于关闭状态.

我正在使用以下代码.这是在委托中使用线程的正确方法吗?我仍然在使用invoke()方法,它不能正常工作.请用示例代码进行说明.

i have created Form1.in Form1 we had one menu strip. by e clicking on menu strip we have to open another form say Form2 and Form1 should be in deactivated state still Form1 closed.

i am using Following code . Is this the correct way of using threads with delegation. still i am using invoke() method it is not working fine . please explain with sample code.

 public delegate void OpenEnv();
    public partial class Home : Form
    {
	............
         public Home()
        {
            InitializeComponent();
            
        }
         private void cCEnvironmentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(OpenEnv));
            t.Start();

            //CreateEnvironment ce = new CreateEnvironment(working_directory);
            //ce.Show();
        }
        public void OpenEnv()
        {
            OpenEnv createenv_delinst = new OpenEnv(opennewform);
            this.Invoke(createenv_delinst);
           
            //CreateEnvironment ce = new CreateEnvironment(working_directory);
            //ce.Show();
        }
        void opennewform()
        {
           
            CreateEnvironment ce = new CreateEnvironment(working_directory);
            ce.Show();
         }
}

推荐答案

这听起来像是一个愚蠢的问题,但是为什么要这么做呢?如果您要执行的操作是我们必须打开另一个表单,则Form2和Form1应该处于停用状态,而Form1仍处于关闭状态". (我认为这意味着直到Form2关闭"),然后才使用ShowDialog:
This may sound like a silly question, but why are you doing that at all? If what you are after is "we have to open another form say Form2 and Form1 should be in deactivated state still Form1 closed." (which I assume means "until Form2 closed") then just use ShowDialog:
CreateEnvironment ce = new CreateEnvironment(working_directory);
ce.ShowDialog();

并且忘记所有线程,Form1将一直处于不活动状态,直到Form2关闭,此时将继续执行ShowDialog方法调用之后的指令.

还是我缺少什么?

And forget all the threading, Form1 will be inactive until Form2 closes at which point execution will continue with the instruction following the ShowDialog method call.

Or am I missing something?


使用以下代码在单击菜单栏时打开任何表单...
我希望这是打开和关闭表格的唯一问题.
use the following code to open any form on click of a menustrip...
I hope this is the only matter of opening and closing the forms.
Form1 _form1 = new Form1()
this.Hide();
_form1.ShowDialog();
this.Show();


或者如果您愿意,可以指定表单的不透明度
喜欢...


or if you want you can specify the opacity of the forms
Like ...

Form1 _form1 = new Form1()
this.Opacity = 0.1;
_form1.ShowDialog();
this.Opacity = 1.0;


这篇关于在单独的线程中打开新表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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