I'm trying to release a lock after some timeout. The lock is acquired to do any database changes so that multiple requests will not update the database simultaneously.
Below is the code
public static readonly _lockObject = new Object();
public bool UpdateData(ModifiedData modifiedData)
{
var success = false;
lock(_lockObject)
{
var thread = new Thread(()=>
{
try
{
SessionManager.Instance.OpenConversation(true); // Starts a session using nHibernate
//Modifications
success = true;
SessionManager.Instance.EndConversation(true);
}
catch(ThreadAbortException ex)
{
//Do I need to handle any condition here
}
});
thread.Start();
if (!thread.Join(TimeSpan.FromMilliseconds(1000))) // Timeout
{
if (!success)
thread.Abort();
else
{
thread.Join();
}
}
else
{
if (!success)
{
throw new Exception("Should Not occur");
}
}
}
}
are there any better approaches available?? I tried with Action.BeginInvoke but in that not able to kill or stop action execution after some timeout.
lockis acquired" actually read "The lock is required"? \$\endgroup\$