MAME frontend again

Worrow

Regular
Joined
Jul 10, 2003
Messages
68
Recently, I translated the code from VB.NET 2003 to C# 2005. I thought it should work the way like its VB counterpart, but no...here is the code to start the process:

ProcessStartInfo startInfo = new ProcessStartInfo("mame.exe");
startInfo.Arguments = listView1.SelectedItems[0].Text;
startInfo.UseShellExecute = true;
Process proc = Process.Start(startInfo);
proc.WaitForExit();

Unlike the VB version, every VERY FIRST time I start mame, the mouse pointer disappears, the whole Windows is freezing(almost). I can't do anything but CltAltDel to stop the MAME process. Then I try again (without close the frontend) and it works! Even if I close the frontend and re-start again, it's working too.

I have no idea what is going on here, any pointer? :confused:
 
Keep in mind that the conversion was not only from VB to C# but also from 2003 to 2005. It is quite possible that the problem is a result of the different version of .Net. The behavior you describe to me doesn't seem all that surprising, considering that last line, but of course, I can't see the rest of the program.

That last line, proc.WaitForExit(), will block the thread that started MAME until MAME closes. If that is the application's main thread or UI thread then it is very possible to introduce a race condition. As an example, your program's main thread is frozen, and it will stay that way until mame.exe closes, now suppose mame tries to obtain some information about the application that started it running, and in the process, MAME, Windows, or the .Net framework needs a function to be run on your main thread. Now your main thread is waiting for MAME, and MAME is waiting for your main thread, and no one gets anywhere.

It shouldn't be an issue if MAME was executed from another thread, and even if it is run from the main thread that isn't necessarily exactly what your problem is, but I would say it is a possibility. Try running the code you posted from another thread (if you don't know much about threading, just check out the Thread class in MSDN).
 
Rather than wait for the process to end use Exited event to find out when the process has ended, that way messages can continue processing while the process has been launched.

C#:
internal void ProcessExited(object sender, System.EventArgs e)
{
	((Process)sender).Close();
	MessageBox.Show("Mame Process has ended");
}				

private void Button_Click(object sender, System.EventArgs e)
{
	Process myProcess = new Process();
	myProcess.StartInfo.FileName = "mame.exe";
	myProcess.StartInfo.Arguments = listView1.SelectedItems[0].Text;
	myProcess.EnableRaisingEvents = true;
	myProcess.Exited += new System.EventHandler(ProcessExited);
	bool ret = myProcess.Start();
}
 
Wow, thanks to both of you. I had never considered there is a race condition going on! :o Thanks again!
 
Back
Top