c#: help me pinpoint my unsafe thread object!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • matthewaveryusa
    New Member
    • Jul 2008
    • 20

    c#: help me pinpoint my unsafe thread object!

    okay so I have an app that updates it's status every second to run tests and grab information from various files/places on a system. My app works great but I get a funny error from time to time: System.InvalidO perationExcepti on "object is being used" (or something similar).

    every second a timer sets off an event. this event creates a thread to run the tests as to not freeze the GUI. However before the test is run, it fetches a static boolean that says whether a thread like this is already running or not. if the bool returns true then the thread is not initialized.


    here is the code for the timer event:

    Code:
            private void scheduler_event(object source, System.Timers.ElapsedEventArgs e)
            {
                DateTime static_time_now = DateTime.Now;
                bool pre_busy = constants.busy;
                constants.busy = true;
    
                if (constants.firstrun == true)
                {
                    constants.firstrun = false;
                    constants.busystatus = new bool[] { false, false, false };
                    constants.updatestatus = new bool[] { true, true, true };
                    Thread firstrunthread = new Thread(firstrun);
                    firstrunthread.Start();
                }
    
                    if (pre_busy == false)//when program is fired up, prebusy is set to false
                {
                    constants.updatestatus[0] = ((constants.next_sysinfo <= static_time_now) && (constants.busystatus[0] == false));
                    constants.updatestatus[1] = ((constants.next_drive <= static_time_now) && (constants.busystatus[1] == false));
                    constants.updatestatus[2] = ((constants.next_eventlog <= static_time_now) && (constants.busystatus[2] == false));
               
                    Thread runthread = new Thread(run);
                    runthread.Start();
                }
    
            }
    constants is an abstract class with static methods I use as global values.

    now in my run code, once the test has finished, I set
    Code:
    constants.busy = false
    can this be the problem? If yes, how do I get rid of it?
  • danrabydotcom
    New Member
    • Aug 2008
    • 11

    #2
    I am also new to the C# world, but I am writing a similar app.

    What I've done is created a thread with a loop in it. This way you do not have to worry about multiples threads colliding.
    Code:
    public void threadMethod()
    {
              //Init Code - Things to happen only on the first run.
              Thread.Sleep(1000);
              //Now the loop
              while (condition)
              {
                        // Get Stats
                        Thread.Sleep(1000);
    
              }
    }
    Last edited by danrabydotcom; Aug 12 '08, 03:56 AM. Reason: Errors In Code

    Comment

    • matthewaveryusa
      New Member
      • Jul 2008
      • 20

      #3
      yes but this will freeze my Gui, especially when I call Perl scripts in my testing that take 10 minutes to complete.

      I found the answer to my question:

      I lock my whole constants class (this is the class with my static methods/variables) whenever I change busy.
      so instead of:

      Code:
      constants.busy = false;
      I have:

      Code:
      lock(constants){
      {
      constants.busy = false;
      }
      }
      app running for 48 hours and no problems... looking good!

      Comment

      • danrabydotcom
        New Member
        • Aug 2008
        • 11

        #4
        Great!

        I see what you mean now. I wasn't sure how long it would take to process your requests.

        Comment

        Working...