3 Replies - 553 Views - Last Post: 13 January 2012 - 07:34 AM Rate Topic: -----

#1 boom_bread  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-January 12

Check if an object is used within loop / multithreading

Posted 12 January 2012 - 08:08 PM

Hi,

I tried to find around the same problems as per above title, but failed. I did not find any ideas through google as well, sorry if I was missing something.

But appreciate your idea / confirmation on this.

Details of the idea is, I have a class MyClass and function within it is DoThisFunction(), then I would compile it in a library. I want to prevent people who use it in some loops or multithreading:

MyClass a = new MyClass();
if(xxxx)
{

a.DoThisFunction();
}



Is it possible to have some properties in MyClass so that it could detect any presence of the object within loop?

Thanks in advance! :)

Is This A Good Question/Topic? 0
  • +

Replies To: Check if an object is used within loop / multithreading

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Check if an object is used within loop / multithreading

Posted 12 January 2012 - 09:21 PM

Well, there's a fairly easy way to prevent your method from being called more than once at a time. A loop wouldn't have this problem, since loops don't run simultaneously. Threads can.

There are probably a few ways to do this. One of the simplest would be to make a private static bool in your class. Something like this:

private static object _locker = new object();
private static bool _isInUse = false;
private static bool IsInUse {
  get { return _isInUse; }
  set {
    lock(_locker){
      _isInUse = value;
    }
  }
}


Then you could check IsInUse at the beginning of your method. If it's false, set it to true and proceed, then set it to false when you're finished. If it's already true, exit (or throw an exception, or whatever).

Of course, you could skip that whole nonsense and just gain a lock as you enter the method, assuming you're OK with all the calls proceeding (just not at the same time, one would wait for the other to finish).

There's a more drastic solution as well, called a Mutex. You can go so far as to gain a global mutex that will only allow one call of your library at a time system-wide, so that you can't run multiple instances of an application to get around your limit.
Was This Post Helpful? 2
  • +
  • -

#3 boom_bread  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-January 12

Re: Check if an object is used within loop / multithreading

Posted 13 January 2012 - 12:08 AM

Hi Curtis,

thanks a lot for the codes and thorough explanation. It indeed helps a lot :)

I think I'll skip for Mutex but keep in mind for future reference. It's surely drastic as user might think of another solution that is to create another instance to bypass the loop/multithreading.

Yes, lock is good enough but static boolean provided is way to go! :)


As for the loop, I was thinking to put timespan in the class properties, so that if the class is instantiated in let say, loop, it will check for how many intervals, (let say 3 secs).

If less than that, it's most probably being called in loop right, thus then I would just throw error or something in the class.

Do you think it's practical?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,372
  • Joined: 02-June 10

Re: Check if an object is used within loop / multithreading

Posted 13 January 2012 - 07:34 AM

Personally I'd have to ask *WHY*?

If running the method in a loop or thread creates problems, then fix the method.

If the method can run fine in loop or thread, then why do you want to cripple its use?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1