Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,456 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,933 people online right now. Registration is fast and FREE... Join Now!




I want to add threads to my program, but I have no idea how to correct

 
Reply to this topicStart new topic

I want to add threads to my program, but I have no idea how to correct

paulwis
4 Aug, 2008 - 02:40 PM
Post #1

New D.I.C Head
*

Joined: 26 Jul, 2008
Posts: 6


My Contributions
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 biggrin.gif ).

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 biggrin.gif

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: I Want To Add Threads To My Program, But I Have No Idea How To Correct
4 Aug, 2008 - 04:29 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,210



Thanked: 214 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well first you can try looking at my blog where I cover how to setup a thread with a progress bar, use a delegate to do it, and instead of processing the file like I do you would do your image processing.

Martyr2's Programming Underground - Threads, Progressbar and file processing in C#

You can also check out our tutorials section where we have a threading tutorial by PsychoCoder located here...

Cross Thread Communication in C#

These two can get you started and hopefully help you setup the progress bar to monitor the task.

smile.gif
User is online!Profile CardPM
+Quote Post

paulwis
RE: I Want To Add Threads To My Program, But I Have No Idea How To Correct
4 Aug, 2008 - 07:41 PM
Post #3

New D.I.C Head
*

Joined: 26 Jul, 2008
Posts: 6


My Contributions
Your blog was extremely helpful. I am running into a few quirks, however. I have the program run the thread, then after that it attaches the imagelist object to the listview object. I had to do it this way to stop one thread from accessing another. Unfortunately, the thread executes, then performs that action right after, when what I need it to do is execute once the thread is done. Is there a way to halt the main program execution, but still have a living form? I tried the Join() method, but that only froze the main form AND caused the thread to not execute.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:30PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month