using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static String nProcessID;
        static int count = 0;
        public static void process_Exited(object sender, EventArgs e)
        {
            Console.WriteLine(nProcessID);
            //Console.WriteLine(“exit”);
        }

        public static void call_process_ffmpeg(object name)
        {
            string thread_name = (string)name; //unboxing
            Process process = new Process();
            //Environment.GetEnvironmentVariable("WinDir") + "\\Notepad.exe";
            //  process.StartInfo.FileName = "C:\\ffmpeg\\bin\\ffmpeg.exe -re -i rtmp://localhost/live/myCamera -s 640x480 -vcodec copy  -tune  zerolatency - filter_complex aresample = 44100 - bufsize 1000  - c:a aac -b:a: 0 128k - f flv rtmp://localhost/livepkgr/livestream?adbe-live-event= liveevent";
            ProcessStartInfo pinfo = new ProcessStartInfo("ffmpeg");
            pinfo.Arguments = "-re -i rtmp://localhost/live/myCamera -r 30 -vcodec copy  -tune  zerolatency  -filter_complex aresample=44100  -bufsize 1000 -c:a aac -b:a:0 128k -f flv rtmp://localhost/livepkgr/livestream?adbe-live-event=liveevent";
            process.StartInfo = pinfo;

            process.Start();

            process.Exited += new EventHandler(process_Exited);

            process.EnableRaisingEvents = true;
            // process.WaitForInputIdle();

            nProcessID = process.SessionId.ToString();

            process.WaitForExit();
            Console.WriteLine(thread_name + "exit");
        }
        static void Main(string[] args)
        {
            List<Thread> threads = new List<Thread>();

            for (int i = 0; i < 10; i++)
            {
                Thread tmp = new Thread(new ParameterizedThreadStart(call_process_ffmpeg));

                threads.Add(tmp);

            }
            int count = 0;
            foreach (Thread tmp in threads)
            {
                tmp.Start(count.ToString());
                count++;
            }

            //for (int i = 0; i < threads.)
            //Thread test = new Thread( new ParameterizedThreadStart(call_process_ffmpeg));

            //test.Start("test1");
            //Thread test2 = new Thread(new ParameterizedThreadStart(call_process_ffmpeg));

            //test2.Start("test2");
            //Console.ReadLine();


        }

    }
}