Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a Process in a Thread

Tags:

c#


FURTHER EDIT the following is not production code - I'm just playing around with a couple of classes trying to figure out how I run processes within threads - or even if that is viable. I've read various definitions on MSDN but am a newbie to threads and processes so any further definitive references to articles would be appreciated


this is fine...

class Program {
    static void Main(string[] args) {

        Notepad np = new Notepad();
        Thread th = new Thread(new ThreadStart(np.startNPprocess));
        th.Start();

        Console.WriteLine("press [enter] to exit");
        Console.ReadLine();
    }
}

public class Notepad {

    public void startNPprocess() {

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

    }
}

this isn't...

class Program {
    static void Main(string[] args) {


        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();


        Console.WriteLine("press [enter] to exit");
        Console.ReadLine();
    }
}

Why does the second not do the same as the first? In the second script I'm trying to pass Process.Start using the Threadstart delegate ...I thought this would be ok as its a void method? Is the first script the only option or can I change the second slightly so that it effectively does the same job as the first i.e start an instance of Notepad in a specified thread?


EDIT

Some background as to why I'm playing around with this code: ultimately I need to build an application which will be running several Excel processes simultaneously. These processes can be troublesome when VBA errors as it results in a dialogbox. So I thought if each process was running in a thread then if a particular thread has been running for too long then I could kill the thread. I'm a newbie to Threads/Processes so basically playing around with possibilities at the moment.

like image 444
whytheq Avatar asked Jan 22 '13 09:01

whytheq


3 Answers

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.

like image 81
spender Avatar answered Nov 10 '22 01:11

spender


you can make changes like

ThreadStart ths = new ThreadStart(delegate() { pr.Start(); });
like image 32
Rahul Vasantrao Kamble Avatar answered Nov 09 '22 23:11

Rahul Vasantrao Kamble


Just start the process normally using this code:

Process.Start("notepad.exe");

There is no point and no benefits in creating a thread to run a new process. It's like running a batch file that executes "cmd.exe" when you can directly execute "cmd.exe"... you are just doing more than what's necessary for nothing. Don't reinvent the wheel and play easy :P

like image 24
Tommaso Belluzzo Avatar answered Nov 10 '22 00:11

Tommaso Belluzzo