I can’t say i fully understood the concept of threads even i read so many articles over it, i’m a bit thick and what i want is thread safe parameters. I’m sending string arguments to a thread that i’m using ThreadPool.QueueUserWorkItem to start but i’m using the same thread just after it again with another arguments.
What i want from it to be able to process different threads with different arguments but its not stable probably because i’m changing the parameter string just after calling the first thread. My instincts tell me to use Lock but don’t know how to and where..
Oh btw the output of this code is usually 3 threads working with the latest parameter (which is configurations for 200p)
The code i use to call my thread is this;
processThread pt = new processThread();
pt.fileName = AppDomain.CurrentDomain.BaseDirectory + "bin\\ffmpeg.exe";
pt.filePath = Path.Combine(Vci.Core.Sandbox.UploaderControlSandboxPath, fileGuid);
pt.vidPathHigh = AppDomain.CurrentDomain.BaseDirectory + "videos\\480p\\" + fileGuid + ".wmv";
pt.vidPathMid = AppDomain.CurrentDomain.BaseDirectory + "videos\\360p\\" + fileGuid + ".wmv";
pt.vidPathLow = AppDomain.CurrentDomain.BaseDirectory + "videos\\200p\\" + fileGuid + ".wmv";
if (height >= 480)
{
newHeight = (int)Math.Floor(480 * aspectRatio);
initArgs = "-i " + pt.filePath + " -vcodec wmv2 -qscale 2 -s " + newHeight + "x480 -acodec wmav2 -ar 44100 -ab 128k -y \"" + pt.vidPathHigh + "\"";
ThreadPool.QueueUserWorkItem(o => pt.callExecute(initArgs));
newHeight = (int)Math.Floor(360 * aspectRatio);
initArgs = "-i " + pt.filePath + " -vcodec wmv2 -qscale 4 -s " + newHeight + "x360 -acodec wmav2 -ar 44100 -ab 128k -y \"" + pt.vidPathMid + "\"";
ThreadPool.QueueUserWorkItem(o => pt.callExecute(initArgs));
newHeight = (int)Math.Floor(200 * aspectRatio);
initArgs = "-i " + pt.filePath + " -vcodec wmv2 -qscale 6 -s " + newHeight + "x200 -acodec wmav2 -ar 44100 -ab 128k -y \"" + pt.vidPathLow + "\"";
}
And code to my thread Class is this;
public class processThread
{
public string filePath { get; set; }
public string fileName { get; set; }
public string vidPathHigh { get; set; }
public string vidPathMid { get; set; }
public string vidPathLow { get; set; }
public void callExecute(Object o)
{
try
{
executeProcess(fileName, o as string);
}
catch (ThreadAbortException abortException)
{
// do something
}
}
private void executeProcess(string fileName, string arguments)
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = fileName;
myProcess.StartInfo.Arguments = arguments;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.RedirectStandardOutput = false;
try
{
myProcess.Start();
}
catch (Exception ex)
{
throw;
}
myProcess.WaitForExit();
myProcess.Close();
}
}
Thanks in advance any help is appreciated!
The issue is that you are “capturing”
initargsvia the use of the lambda expression.You shouldn’t be reusing
initargsin this way. Your code will be more readable and easier to maintain, and you will avoid this problem.So, first, use two different instances of
initargs:Second, there’s a lot of needless repetition when you assign to
initArgs. That’s not fun to maintain. This should get you started on a clearer version:You can do more and use
String.Formatto really clean the whole thing up.Then you can say
All that said, I don’t understand why you are using threads at all if the threads are merely starting new processes. Just start the processes directly, and wait for them all to finish.