简体   繁体   English

c#从控制台运行Windows窗体并运行它们

[英]c# run windows form from console and run both of them

There is a console application that simulates router functionality (packet swithcing and so on). 有一个控制台应用程序可模拟路由器功能(数据包切换等)。 I made a simple Windows Form that should show how much each router's socket is used. 我制作了一个简单的Windows窗体,该窗体应显示每个路由器的套接字使用了多少。 Each socket has different capacity and each Form "connected" to socket should show how much capacity there is still available. 每个套接字具有不同的容量,并且每个“已连接”到套接字的表格都应显示仍有多少容量。

My code so far: 到目前为止,我的代码:

static void Main(string[] args)
        {
        //get number of router's sockets
        args = Environment.GetCommandLineArgs();
        int socketnumber = Convert.ToInt32(args[2]);
        //get sockets' capacity
        int[] array = new int[socketnumber];
        for (int i = 0; i < socketnumber; i++)
        {
            array[i] = Convert.ToInt32(args[3 + i]);
        }

Now, LRM is a WinForm that shows each socket's (or, more accurately, link's attached to socket) status, so I initialize it and give it parameters. 现在,LRM是一个WinForm,它显示每个套接字的状态(或更准确地说,链接附加到套接字的状态),因此我对其进行初始化并为其指定参数。

            LRM[] lrm = new LRM[socketnumber];

            for (int i = 0; i < socketnumber; i++)
            {
                lrm[i] = new LRM();
                lrm[i].Show();
                a++;
            }

            for (int i = 0; i < socketnumber; i++)
            {
                Console.WriteLine(array[i]);
                lrm[i].capacity = array[i];
                lrm[i].available = array[i];
                lrm[i].WriteCapacity(lrm[i].capacity);
                lrm[i].WriteAvailable(lrm[i].available);
            }

WriteCapacity and WriteAvailable are methods that are invoked by router to update values on LRM, like that: 路由器调用WriteCapacity和WriteAvailable来更新LRM上的值的方法,如下所示:

    public void WriteAvailable(int ava)
    {
    MethodInvoker mi = delegate ()
       {
        textBox2.Text = ava.ToString();
       };
    Invoke(mi);
    }

Now, current problems I have: 现在,我有当前的问题:

1) Running multiple windows forms from console AND maintaining their functionality (all of them simultaneously), when number of windows forms varies (is set by command line argument) 1)当Windows窗体的数量发生变化(由命令行参数设置)时,从控制台运行多个Windows窗体并维护其功能(同时执行所有功能)

2) Updating LRM values. 2)更新LRM值。 When using show() method it just displays the form, it does not give it any values, and soon after form stops to respond, giving windows error. 当使用show()方法时,它仅显示表单,它不提供任何值,并且在表单停止响应后不久,出现Windows错误。 When using Application.Run() method, one form runs nicely, but neither other LRMs nor router's console works. 当使用Application.Run()方法时,一种形式可以很好地运行,但是其他LRM和路由器的控制台都无法工作。

I appreciate Alexander Petrov's answer, but I found a way to solve my problem. 我感谢亚历山大·彼得罗夫(Alexander Petrov)的回答,但我找到了解决问题的方法。

I tried Thread approach. 我尝试了线程方法。 First I made most of my variables static: 首先,我将大多数变量设为静态:

    static int[] array;
    static LRM[] lrm;
    static int port;

then I made method that will be acting as thread starting method 然后我做了一个将作为线程启动方法的方法

    private static void startLRM(int i)
    {
        Console.WriteLine(i);
        lrm[i].Text = "LRM R" + ((port / 10) - 120).ToString() + " S" + a.ToString();
        a++;

        Console.WriteLine(array[i]);
        lrm[i].capacity = array[i];
        lrm[i].available = array[i];
        lrm[i].WriteCapacity(lrm[i].capacity);
        lrm[i].WriteAvailable(lrm[i].available);
        lrm[i].ShowDialog();
    }

Then, in Main() method, I allocated memory for link resource managers,and in for loop I made as many LRMs as were needed. 然后,在Main()方法中,我为链接资源管理器分配了内存,在for循环中,我制作了所需数量的LRM。

        lrm = new LRM[socketnumber];

        for (int i = 0; i < socketnumber; i++)
        {
        lrm[i] = new LRM();
        Thread thread = new Thread(() => startLRM(i));
        thread.Start();
        Thread.Sleep(150);
        }

Sleeping was actually necessary, because for unknown reasons I got errors when it was not there. 睡觉实际上是必要的,因为由于未知的原因,当我不在时会出现错误。

Finally, I get what I needed - a console application, which shows what is happening, and few windows forms that shows in nice manner some values. 最终,我得到了我需要的-一个控制台应用程序,它显示了正在发生的事情,以及几个以漂亮的方式显示一些值的Windows窗体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM