I have completed a program, and have been adding bells and whistles to it. Well, I'm now biting off more than I can chew with my latest feature. I created a program that takes a directory of a specific kind of video file called a "sheep", creates an array of the paths of each video in that directory, generates thumbnails for each video via an external call to ffmpeg, then adds those to an imagelist object, to be later used in a listview object (you may have to re-read that a couple times

).
The main downside to my program is that while these thumbnails are being generated, my application becomes unresponsive, and this goes on for 5-10 minutes, depending on how many thumbnails need to be generated. I want to add a progress bar to the main form showing how much longer the operation will take, but it won't mean anything if the form is not updating. I know I have to take the plunge and add threads to my application.
In my application, I had a generateThumbs() method, which references the file path array many times. This is the method that takes a while to complete. Reading the MSDN articles on threading, I saw that I needed to create another class. So I created a SheepLoader() class and moved the generateThumbs() method inside that. Of course, some variables are no longer accessible from inside this new class, which causes all sorts of compiler errors. I also get static method errors, which are the most frustrating things everrrrrrrrrrr, when trying to create threads and such.
Here is some of my embarassing code:
The thread declaration in the main form class
CODE
SheepLoader sl = new SheepLoader();
Thread slt = new Thread(new ThreadStart(sl.generateThumbs));
The SheepLoader() class
CODE
public class SheepLoader
{
public void generateThumbs()
{
//if the sheep thumbnail directory does not exist, create it
if (!Directory.Exists(Properties.Settings.Default.SheepDirectory + "\\ShoopThumbnails"))
{
Directory.CreateDirectory(Properties.Settings.Default.SheepDirectory + "\\ShoopThumbnails");
}
int n = 0;
//use ffmpeg to take single frame screenshots
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = programPath + "\\Resources\\ffmpeg.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
while (n < sheepFilePathArray.Length)
{
if (!System.IO.File.Exists(Properties.Settings.Default.SheepDirectory + "\\ShoopThumbnails\\" + sheepFileNameArray[n] + ".jpg"))
{
p.StartInfo.Arguments = " -i " + sheepFilePathArray[n] + " -f image2 -vframes 1 -ss 2.5 -s 96x72 " + Properties.Settings.Default.SheepDirectory + "\\ShoopThumbnails\\" + sheepFileNameArray[n] + ".jpg";
p.Start();
p.WaitForExit();
}
n++;
}
}
}
Some of the major errors:
A field initializer cannot reference the non-static field, method, or property 'Shoop.Form1.sl'
The name 'generateThumbs' does not exist in the current context (easy to see why)
The name 'sheepFileNameArray' does not exist in the current context (again, easy to see why)
I don't know how to use threads without having to make everything static. I don't know how to access the file path array from the other class, and I read its poor programming practice to instantiate the whole class to get inside it. I don't know how to use threads in general =(. Can someone please guide me in the right direction? I would love you forever