Click here to Skip to main content
14,297,656 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi fellow CP'ians,

i encountered a new problem while thinking about multi user software. Sohort background story...

I have in mind that the upcomming multiuser system should and will run with an observer to dedicate massages and updates to the software. But currently i don't get the timeslot to implement that, that's why i went on another approach using locks (that get stored into the DB).

Soooo, now im thinking about different scenarios like a project manager sets the project into edit mode (locks get set for every object in the project) and there is someone already working on an object. In this case i can't lock it for the PM and have to notify him (will use a small icon) but i have to recheck till the other dude closed his edit and the PM can finally lock it.

For this i wanted to use a seperate thread that querys the DB regularly till the object can be locked.

Now to my Question:

Since i worked a bit with threads i realised that it won't work that smooth if you leave a method in which you created a thread. So just for me as input. Is my feeling right?

I would try to set up like this

public bool LockProject(GUID InProjectID)
{
    bool _result = false;

    //Lock the Project
    /*Do some code*/

    //Check if locks can't be set and remove the double locks
    /* Do some code*/

    //Start the Thread to continuously ask if a lock still persists
    /*Initialize Thread and let it run the check method*/

    return _result;
}


So would this "kill" my thread? I expect it to return true, since we could lock the project for others but still need to check. So if i leave the method, will the Thread get dumped by the garbage collection or would this work?

Otherwise, where would i have to put the Thread? Is this realisable with this Task thing i read here?

I don't need code, just a push in the right direction or if my thoughts are correct a confirmation.

Thanks in advance :)

What I have tried:

As written above, the thoughts about how to handle this issue.
Posted
Updated 23-Mar-16 22:15pm
v2
Comments
RickZeeland 18-Mar-16 14:21pm
   
I think your thread will keep running after leaving the method.
Only when the application terminates and the thread .IsBackground property is set to true it will be terminated by the application.
But the best way to find out is of course to write a small console application and test it out.
Jitesh Hirani 21-Mar-16 9:04am
   
For this i wanted to use a seperate thread that querys the DB regularly till the object can be locked.

Why to query your database continuesly while you can use
http://www.codeproject.com/Articles/12335/Using-SqlDependency-for-data-change-events
johannesnestler 22-Mar-16 11:09am
   
just a thought: I build a new multiuser system at the moment, and went for a lock-free "optimistic concurrency" approach, very good scaling and perfomance so far. no need for "lock-Messages" and all those nasty problems you get if you use a "locking strategy" (deadlocks?). just my 2c...

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I agree with johannes that an optimistic persistance is a better performing model however, if you are convinced that you want to implement locking I would do it in a slightly different way.

When you want to lock the entity, your lock should acquire the lock for 10 seconds, and every 5 seconds would repeat the process and renew the acquisition. If you encounter any unhandled exception, or failure to release, the object will be automatically released after 10 seconds regardless. (or a time period that makes sense).

Of course if you want to lock an entity across sessions, this is not the approach for you, but worth considering with distributed locking, how failure recovery works.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100