So the answer from keftmedei above works fine .
Here is a full example trying to launch a process from a MVVMLight RelayCommand
Without using Dispatcher, this ViewFile method went off to Tralfamador to visit the plumber's helper people without leaving a forwarding address e.g. EXCEPTION, Will Robinson!!! (see Vonnegut, see Lost In Space...)
/// <summary>
/// Run a file by Process exec - use the shell file association
/// Run it in its own thread by using the Dispatcher
/// </summary>
/// <param name="fileName"></param>
[STAThread]
public static void ProcessExecFileByName(string fileName)
{
try
{
Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
ViewFile(fileName);
}));
}
catch (Exception ex)
{
ex.PreserveStackTrace();
var sb = new StringBuilder();
sb.AppendLine("File to view: " + fileName);
ex.AppendErrorData(sb, System.Reflection.MethodBase.GetCurrentMethod().Name);
throw;
}
}
/// View the file using the Shell file association
private static void ViewFile(string fileName)
{
using (new Helpers.WaitCursor())
{
try
{
Logger.DebugFormat("Starting process with file {0}", fileName);
var proc = new Process();
proc.EnableRaisingEvents = true;
proc.Exited += new EventHandler(ProcessExecExitedHandler);
proc.StartInfo = new ProcessStartInfo()
{
FileName = fileName, //put your path here
UseShellExecute = true,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
};
proc.Start();
Logger.DebugFormat("Process started with file {0}", fileName);
}
// this is permissive
catch (FileNotFoundException fileNFEx)
{
ShowError(fileNFEx.Message, true, "WARN");
}
catch (InvalidOperationException invOpEx)
{
invOpEx.PreserveStackTrace();
throw;
}
catch (IOException IOEx)
{
IOEx.PreserveStackTrace();
throw;
}
catch (System.Security.SecurityException securityEx)
{
securityEx.PreserveStackTrace();
throw;
}
catch (Exception ex)
{
ex.PreserveStackTrace();
throw;
}
finally
{
//if (!ok)
//{
// ShowError(statusMessage);
//}
}
}
return;
}
private static void ProcessExecExitedHandler(object sender, System.EventArgs e)
{
try
{
if (sender == null)
{
return;
}
if (((Process)sender).HasExited == false)
{
((Process)sender).Kill();
}
}
catch (Exception ex)
{
ex.PreserveStackTrace();
throw;
}
Logger.DebugFormat("Process ended with file {0}", ((Process)sender).StartInfo.FileName);
return;
}