0

I have been trying to work with async/await, Thread t = new Thread and every possible things you can imagine about multithreading... Running an async method with Process.Start keep blocking the ui, creating new thread immediately close the process and i dont know why???

If you guys dont mind, may i ask:

-How we succesfully run process.start (ffmpeg) in multithread so it dont block the ui? It actually work on synchronous thread but it open/close as soon as i run it.

I could copy/paste code but actually i tried over 20 methods and none are working:

private static void ProcessFile(string command, bool show)
        {
            try
            {
               
                ProcessStartInfo startInfo = new ProcessStartInfo();
                Thread processStarter = new Thread(delegate ()
                {
                    startInfo.FileName = "cmd.exe";
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = show;
                startInfo.Arguments = $@"/c ffmpeg {command}";
                Process process = new Process();
                process.StartInfo = startInfo;
                process.EnableRaisingEvents = true;
                process.Start();
                process.WaitForExit();
                });
                processStarter.IsBackground = true;
                processStarter.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
Thread myNewThread = new Thread(() => ProcessFile("mycommand", true));
myNewThread.Start();

private static void ProcessFile(string command, bool show)
        {
            try
            {
               
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = show;
                startInfo.Arguments = $@"/c ffmpeg {command}";
                Process process = new Process();
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
private async void ProcessFile(string command, bool show)
        {
            try
            {
               
               await Task.Run (() =>
               {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = show;
                startInfo.Arguments = $@"/c ffmpeg {command}";
                Process process = new Process();
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
                });  
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

I tried almost everything i found on google and sincerely multithread getting more confused by the time.

Can a nice soul give me an hand. Thank you

Well i tried alot of asynchronous code but its even worst than before.

CSharpMan
  • 11
  • 2

0 Answers0