SteamController: Use MyLock to detect the deadlock when handling controller

This commit is contained in:
Kamil Trzciński 2023-01-05 11:46:20 +01:00
parent 082dede416
commit edca1663c8

43
CommonHelpers/MyLock.cs Normal file
View file

@ -0,0 +1,43 @@
namespace CommonHelpers
{
public class MyLock : IDisposable
{
private object context;
public MyLock(object context)
{
this.context = context;
Monitor.Enter(this.context);
}
public MyLock(object lockObj, int millisecondsTimeout)
{
this.context = lockObj;
if (!Monitor.TryEnter(this.context, millisecondsTimeout))
RaiseTimeout();
}
public MyLock(object lockObj, TimeSpan timeout)
{
this.context = lockObj;
if (!Monitor.TryEnter(this.context, timeout))
RaiseTimeout();
}
public void Dispose()
{
Monitor.Exit(context);
}
private void RaiseTimeout()
{
var message = String.Format("Lock took too long for: {0}\n{1}",
this.context, Environment.StackTrace);
Log.TraceError("RaiseTimeout: {0}", message);
MessageBox.Show(message, "Lock timeout");
throw new TimeoutException();
}
}
}