Martin Martin - 3 months ago 19
C# Question

Multithreaded program logic

My professor gave me this semi-pseudo code. He said I should find a mistake somewhere in this code's logic. At the moment I can not find anything, what could be wrong. Could you please give me some hints about what could be wrong? I'm not asking for an answer because I would like to find it myself, but some hints in what direction I should look would be awesome.

class Program
{
int progressValue = 0;
int totalFiles = 0;
int i = 0;
bool toContinue = true;

void MasterThread()
{
Thread thread1 = new Thread(Worker1);
Thread thread2 = new Thread(Worker2);
Thread progressThread = new Thread(ProgressThread);

thread1.Start();
thread2.Start();
progressThread.Start();
}

void Worker1()
{
string[] files = Directory.GetFiles(@"C:\test1");
totalFiles += files.Length;
foreach (string file in files)
{
Encryption.Encrypt(file);
i++;
progressValue = 100 * i / totalFiles;
}
toContinue = false;
}

void Worker2()
{
string[] files = Directory.GetFiles(@"C:\test2");
totalFiles += files.Length;
foreach (string file in files)
{
Encryption.Encrypt(file);
i++;
progressValue = 100 * i / totalFiles;
}
toContinue = false;
}

void ProgressThread()
{
while (toContinue == true)
{
Update(progressValue);
Thread.Sleep(500);
}
}
}

Answer

To add to the good answers already provided, I'm being a little thorough, but the idea is to learn.

Exception Handling

Could be issues with exception handling. Always check your program for places where there could be an unexpected result.
How will this code behave if the value of this variable is not what we are expecting?
What will happen if we divide by zero?
Things like that.

Have a look at where variables are initialized and ask yourself is there a possibility that this might not initialize in the way it's expected?

Exception Handling (C# Programming Guide)

Method Calls

Also check out any libraries being used in the code. e.g. Encryption.
Ask yourself, are these statements going to give me an expected result? e.g.

string[] files = Directory.GetFiles(@"C:\test1");

Will this return an array of strings?
Is this how I should initialise an array of strings?

Question the calls: e.g.

 Update(progressValue);

What does this really do?

Class Library

Threading

How will it work calling three threads like that.
Do they need to be coordinated?
Should threads sleep, to allow other actions to complete?

Also as for accessing variables from different threads.
Is there going to be a mess of trying to track the value of that variable?
Are they being overwritten?

Thread Class
How to: Create and Terminate Threads (C# Programming Guide)

Naming Conventions

On a smaller note there are issues with naming conventions in C#. The use of implicit typing using the generic var is preferred to explicit type declarations in C#.

C# Coding Conventions (C# Programming Guide)

I am not saying that there are issues with all these points, but if you investigate all of these and the points made in the other answers, you will find all the errors and you will obtain a better understanding of the code you are reading.

Comments