Ruan
Ruan

Reputation: 4283

How to use multiple threads of the same class in C#

I have a class with a bunch of methods in. For example

     private int forFunction(String exceptionFileList, FileInfo z, String compltetedFileList, String sourceDir)
        {
            atPDFNumber++;
            exceptionFileList = "";
            int blankImage = 1;
            int pagesMissing = 0;

            //delete the images currently in the folder
            deleteCreatedImages();
            //Get the amount of pages in the pdf
            int numberPDFPage = numberOfPagesPDF(z.FullName);

            //Convert the pdf to images on the users pc
            convertToImage(z.FullName);

            //Check the images for blank pages
            blankImage = testPixels(@"C:\temp", z.FullName);

            //Check if the conversion couldnt convert a page because of an error
            pagesMissing = numberPDFPage - numberOfFiles;
}

Now what im trying now is to access that class in a thread.. but not just one thread, maybe about 5 threads to speed up processing, since one is a bit slow.

Now in my mind, thats going to be chaos... i mean one thread changing variables while the other thread is busy with them etc etc, and locking each and every variable in all of those methods... not going to have a good time...

So what im proposing, and dont know if its the right way.. is this

  public void MyProc()
    {
      if (this method is open, 4 other threads must wait)
    {
      mymethod(var,var);
    }
     if (this method is open, 4 other threads must wait and done with first method)
    {
      mymethod2();
    }
  if (this method is open, 4 other threads must wait and done with first and second method)
       {
          mymethod3();
    }
         if (this method is open, 4 other threads must wait and done with first and second and third method)
        {
          mymethod4();
        }
    }

Would this be the right way to approach the problem of multiple threads accessing multiple methods at the same time?

Those threads will only access the Class 5 times, and no more, since the work load will be equally divided.

Upvotes: 4

Views: 2210

Answers (1)

Jirka Hanika
Jirka Hanika

Reputation: 13529

Yes, that is one of your options. The conditional expression you have, however, should be replaced using the lock statement, or even better, make the method synchronized:

[MethodImpl(MethodImplOptions.Synchronized)]
private int forFunction(String exceptionFileList, FileInfo z, String compltetedFileList, String sourceDir)

It is not really a conditional because there is nothing conditional going on here. The next coming thread must wait and then it must go on. It literally sleeps without executing any instructions and then it is woken up from the outside.

Note also that when you are worried about variables messed up during the parallel execution of a non-synchronized method, this only applies to member variables (class fields). It does not apply to local variables declared inside the method as each thread would have its own copy of those.

Upvotes: 5

Related Questions