运行两个WinForm的同时窗口 [英] Run two winform windows simultaneously

查看:998
本文介绍了运行两个WinForm的同时窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个C#WinForm的(.NET 4.0),每连续运行独立但类似的自动任务的形式。分离的,因为它们是不同的过程/工作流,但在足够类似他们是如何运作的项目共享相同的资源(方法,数据模型,组件等)。

这两种形式是完整的,但现在我不知道如何使每个窗口上推出打开并独立运行运行程序。该计划将是永远在线的部署时。

这似乎有点基本的,但我大部分的开发经验,一直Web应用程序。线程的/ etc还是有点陌生​​。我研究过,但最让我已经找到了答案涉及到用户交互和顺序用例 - 这将只是一个系统连续运行两个不同的过程,这将需要与世界交互的独立

潜在的解决方案,我发现可能涉及到多线程,或者某种MDI,或几个人都建议Do​​ckPanelSuite(虽然在一个超级企业环境之中,下载的第三方文件是谈何容易)

 静态类节目
{
    ///<总结>
    ///的主入口点应用程序。
    ///< /总结>
    [STAThread]
    静态无效的主要()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(假);        //而不是指定frmOne或frmTwo,
        //加载两个WinForms和防止他们逃跑。
        Application.Run(新frmOne());
    }
}


解决方案

您可以创建一个新的的ApplicationContext 重新present多种形式:

 公共类MultiFormContext:ApplicationContext的
{
    私人诠释openForms;
    公共MultiFormContext(PARAMS形式[]形式)
    {
        openForms = forms.Length;        的foreach(以表格形式VAR)
        {
            form.FormClosed + =(S,参数)=>
            {
                //当我们关闭了最后的出发的形式,
                //结束程序。
                如果(Interlocked.Decrement(REF openForms)== 0)
                    了ExitThread();
            };            form.Show();
        }
    }
}

使用,你可以这样写:

  Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Application.Run(新MultiFormContext(新Form1的(),新的窗体2()));

I have two C# winform (.NET 4.0) forms that each run separate but similar automated tasks continuously. Separate in that they are distinct processes/workflows, but similar enough in how they operate to share the same resources (methods, data models, assemblies, etc) in the project.

Both forms are complete, but now I'm not sure how to run the program so that each window opens on launch and runs independently. The program will be "always-on" when deployed.

This might seem a little basic, but most of my development experience has been web applications. Threading/etc is still a little foreign to me. I've researched but most of the answers I've found relate to user interaction and sequential use cases -- this will just be one system continuously running two distinct processes, which will need to interact with the world independently.

Potential solutions I've found might involve multi-threading, or maybe some kind of MDI, or a few folks have suggested the DockPanelSuite (although being in a super-corporate environment, downloading third party files is easier said than done).

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        // Rather than specifying frmOne or frmTwo,
        // load both winforms and keep them running.
        Application.Run(new frmOne());
    }
}

解决方案

You can create a new ApplicationContext to represent multiple forms:

public class MultiFormContext : ApplicationContext
{
    private int openForms;
    public MultiFormContext(params Form[] forms)
    {
        openForms = forms.Length;

        foreach (var form in forms)
        {
            form.FormClosed += (s, args) =>
            {
                //When we have closed the last of the "starting" forms, 
                //end the program.
                if (Interlocked.Decrement(ref openForms) == 0)
                    ExitThread();
            };

            form.Show();
        }
    }
}

Using that you can now write:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MultiFormContext(new Form1(), new Form2()));

这篇关于运行两个WinForm的同时窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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