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.