1

I am very new to threading in .NET and have limited exposure to threading in Java. In short, I have a form that has a System.Windows.Forms.Timer object. The Timer ticks every 1000ms. The event handler merely checks a bool class variable (i.e. processingResponseFiles) to determine whether or not a current operation to process request files is in process. If not in progress, it should process any request files. Otherwise, it should do nothing.

The ProcessRequestFiles method looks at the request and then runs a BackgroundWorker to perform the work. The issue that I am struggling with is that the value of processingResponseFiles is always false even though I am assigning it to true in the first statement of the ProcessingRequestFiles method. Can someone please tell me why the value of processingResponseFiles is always false even though I am setting it in the ProcessRequestFile() method?

I need to protect this method so that it does not get executed more than once.

private void timerRequestTimer_Tick(object sender, EventArgs e)
{
    if (!processingResponseFiles)
    {
        ProcessRequestFile();                
    }

private void ProcessRequestFile()
{
    processingResponseFiles = true;

   // Process Request Files
   // If request type is Synchronize Customers
   // Run thread that synchronizes customers
   // If request type is Synchronize Items
   // Run thread that synchronizes items

   processingResponseFiles = false;
}
Tharif
  • 13,794
  • 9
  • 55
  • 77
Grasshopper
  • 4,717
  • 9
  • 36
  • 62

1 Answers1

2

It isn't very clear, too much stuff commented out, but clearly the flag needs to be reset back to false in the BackgroundWorker.RunWorkerCompleted event handler. Only then do you know that the job was completed. Or just use the BackgroundWorker.IsBusy property.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I definitely think that I need to change the design approach but, can you think of any reason why the class variable processingResponseFiles is always true? Currently, I'm not touching it in any of the threads and every time the Timer gets fired, the value is always the default value of false even though I am setting it to true in the ProcessRequestFile() method on the form. Either way, I'm going to read up more on threading but, I would like to understand what it happening to cause the bool class variable to always = false. – Grasshopper Sep 28 '12 at 21:05
  • You are setting it back to false at the end of the method. Which means that the timer's Tick event handler will **always** see it set to false. As I explained, set it to false in the RunWorkerCompleted event handler instead. – Hans Passant Sep 28 '12 at 21:10
  • Hans Approach is really good but I am not sure the OP understands what it is he trying to do himself – MethodMan Sep 28 '12 at 21:16
  • Thanks, that is the problem. I keep having a hard time remembering that once I run the thread, execution continues through the end of the method. – Grasshopper Sep 28 '12 at 21:17
  • I am deleting my answer since the OP clarified his problem.. thanks – MethodMan Sep 28 '12 at 21:50