admin管理员组

文章数量:1194115

I encountered the following problem, I have a WinFOrm application, if I run the exe directly, everything works. My task is to open my exe from another application that I cannot change.

    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new frmFilter());
    }

If I try to run this exe from under another application in the process manager, I see that the application is running, but I don’t see the form itself.

I changed the code as follows:

  [STAThread]
        static void Main(string[] args)
        {
            var thread = new Thread(() =>
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                Application.Run(new frmFilter());
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();

            var frm = new frmFilter(); // this form showing
            frm.ShowDialog();

        }

I need an explanation why this code shows a form if I run my exe from another application? If I delete even one line, everything will stop working.

Explain why this code works?

If you do it like this it will work, the frmFilter form will be displayed:

 static void Main(string[] args)
        {
            var thread = new Thread(new ThreadStart(() =>
            {
                MessageBox.Show("test"); // If I delete this line, the form will not be displayed, why?
                var frm = new frmFilter();
                frm.ShowDialog();

            }));

            thread.Start();
        }

This is the code of the C++ application that runs my exe in which the form, the form should be on top of the C++ application

CString sPath = GetFilePath( m_sInsFolder ); 

    SHELLEXECUTEINFO ShExecInfo = {0};
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = NULL;
    ShExecInfo.lpFile = m_sParserApp;
    ShExecInfo.lpParameters = sArg;
    ShExecInfo.lpDirectory = sPath;
    ShExecInfo.nShow = SW_HIDE;
    ShExecInfo.hInstApp = NULL;

    int ret = (int)ShellExecuteEx(&ShExecInfo);

I can't change the C++ code

enter image description here

In red I highlighted the C# form that should be on top of C++. In blue is C++ MFC.

My C# code above works but I don't know why. I want a nice solution to have a C# form on top of a C++ application. C# net 4.6.1, C++ is an MFC application.

This code does everything exactly as I need it to show my C# application on top of C++ mfc. But as soon as I delete the line: MessageBox.Show("test"); my form on top of C++ is not displayed. I want to get an answer why my form is not displayed and how to do it correctly so that it is displayed on top of C++ application without MessageBox.Show("test");

static void Main(string[] args)
        {
            var thread = new Thread(new ThreadStart(() =>
            {
                MessageBox.Show("test");
                var frm = new frmFilter();
                frm.ShowDialog();

            }));

            thread.Start();
        }

This code also does what I need, but it looks just awful and I want to know how to do it elegantly and correctly

 [STAThread]
        static void Main(string[] args)
        {
            var thread = new Thread(() =>
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                Application.Run(new frmFilter());
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();

            var frm = new frmFilter(); // this form showing
            frm.ShowDialog();

        }

本文标签: cWin form does not open if you run exe from another applicationStack Overflow