How to start a Process in a Thread

A ThreadStart expects a delegate that returns void. Process.Start returns bool, so is not a compatible signature. You can swallow the return value in by using a lambda that gives you a delegate of the correct return type (i.e. void) as follows:

    Process pr = new Process();
    ProcessStartInfo prs = new ProcessStartInfo();
    prs.FileName = @"notepad.exe";
    pr.StartInfo = prs;

    ThreadStart ths = new ThreadStart(() => pr.Start());
    Thread th = new Thread(ths);
    th.Start();

...but it's probably advisable to check the return value:

    ThreadStart ths = new ThreadStart(() => {
        bool ret = pr.Start();
        //is ret what you expect it to be....
    });

Of course, a process starts in a new process (a completely separate bunch of threads), so starting it on a thread is completely pointless.


you can make changes like

ThreadStart ths = new ThreadStart(delegate() { pr.Start(); });

Tags:

C#