开发者

Can we use another thread to execute a outside executable

开发者 https://www.devze.com 2023-01-26 16:26 出处:网络
I am wondering if it is possible to start another execuatable using anot开发者_开发百科her thread. Start another process is resource intensive. If you\'re asking how to launch the process in the backg

I am wondering if it is possible to start another execuatable using anot开发者_开发百科her thread. Start another process is resource intensive.


If you're asking how to launch the process in the background (so that your UI doesn't freeze), you can write

ThreadPool.QueueUserWorkItem(delegate { Process.Start("notepad.exe"); });

If you're asking to execute the process inside your process' space, that's utterly impossible for arbitrary programs and a very bad idea for managed programs.


If you start another executable from another thread, you will start a new process. I think you are confusing the relationship between threads, processes and executables somewhat.


By nature, an executable must run in it's own process. You could however launch a method from another thread in an executable since it itself is just an assembly.

Threads share the address space of the process that created it; processes have their own address.

To do so, you'd need to make the referenced exe a friend assembly. See here.

Or use remoting.


http://msdn.microsoft.com/en-us/library/e8zac0ca(v=VS.90).aspx

Straight out of MSDN Documentation

        Process myProcess = new Process();

        try
        {
            myProcess.StartInfo.UseShellExecute = false;
            // You can start any process, HelloWorld is a do-nothing example.
            myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
            // This code assumes the process you are starting will terminate itself. 
            // Given that is is started without a window so you cannot terminate it 
            // on the desktop, it must terminate itself or you can do it programmatically
            // from this application using the Kill method.
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

Is that what you mean?

0

精彩评论

暂无评论...
验证码 换一张
取 消