C # threading: console app on new thread is invisible

I load the build of a console app from a resource into a memory stream (later into a byte array) and do the build on a new thread. Note that the container assembly is standard Winforms.

The console application is running on a new thread, but it is not visible. If the console app is replaced with a winforms assembly, the main form is visible and works correctly. If I run the console app from disk as a test, it loads fine and visible.

Any ideas?

ThreadStart _thdInvoke;
Thread _thdMain;
MethodInfo _methodInfo;

      

incision

/* memoryStream is the console application loaded from embedded resource */
var assembly = Assembly.Load(memoryStream.ToArray());
_methodInfo = assembly.EntryPoint;
_thdInvoke = InitializeEp;
_thdMain = new Thread(_thdInvoke);
_thdMain.Start();

      

incision

private void InitializeEp()
    {
        try
        {
            _methodInfo.Invoke(null, null);
        }
        catch (Exception)
        {

        }
    }

      

Please note that the main method of the console application has been changed so that it has string[] args

been removed.

Thanks for the help!

+3


source to share


1 answer


It is "invisible" because the console window does not actually exist. Nothing told the OS to create a console and redirect all read / write to it.

It seems you need to do some customization to create a console window for your application.



Yours is Main()

running, but all calls are Console

completely ignored and reading is not blocked.

I tested the code you had and verified that your program should work (minus the console window). You can check this by taking void Main()

and changing it to int Main()

and then returning some number. _methodInfo.Invoke(...)

will return the return value int Main()

.

+1


source







All Articles