/// <summary> /// Event handler that is called when the timer elapses. /// </summary> private void keepAliveTimer_Elapsed( object sender, System.Timers.ElapsedEventArgs e ) { lock ( this ) { using ( AdoNetSqlUpdater upd = new AdoNetSqlUpdater( @"SELECT * FROM [KeepAlives] WHERE [Username]=@UserName AND [UserDomainName]=@UserDomainName AND [UserWorkstationName]=@UserWorkstationName AND [MainWindowHandle]=@MainWindowHandle ", new AdoNetSqlParamCollection( AdoNetSqlParamCollection.CreateParameter( "@UserName", System.Environment.UserName, DbType.String, System.Environment.UserName.Length ), AdoNetSqlParamCollection.CreateParameter( "@UserDomainName", System.Environment.UserDomainName, DbType.String, System.Environment.UserDomainName.Length ), AdoNetSqlParamCollection.CreateParameter( "@UserWorkstationName", System.Environment.MachineName, DbType.String, System.Environment.MachineName.Length ), AdoNetSqlParamCollection.CreateParameter( "@MainWindowHandle", MainWindowHandle, DbType.Int32 ) ) ) ) { DataRow row = upd.CheckGetOrAddNewRow(); row["LastDate"] = DBHelper.WriteField( DateTime.Now ); row["Username"] = DBHelper.WriteField( System.Environment.UserName ); row["UserDomainName"] = DBHelper.WriteField( System.Environment.UserDomainName ); row["UserWorkstationName"] = DBHelper.WriteField( System.Environment.MachineName ); row["MainWindowHandle"] = DBHelper.WriteField( MainWindowHandle ); upd.Update( AdoNetSqlUpdater.IdentityControl.DontGet ); } } }
public void Store() { using ( AdoNetSqlUpdater upd = new AdoNetSqlUpdater( "GetDummyByID", new AdoNetSqlParamCollection( AdoNetSqlParamCollection.CreateParameter( "@ID", ID, DbType.Int32 ) ) ) ) { DataRow row; if ( upd.Row == null ) { row = upd.AddNewRow(); Store( row ); id = upd.Update( AdoNetSqlUpdater.IdentityControl.Get, "Dummys" ); } else { row = upd.Row; Store( row ); upd.Update( AdoNetSqlUpdater.IdentityControl.DontGet ); } } }
/// <summary> /// Mark as locked. Throws if already locked. /// </summary> private void Lock() { if ( true ) { DateTime dt = DateTime.Now.AddSeconds( -ApplicationConfiguration.Current.KeepAliveSeconds * 2 ); DataRow row = AdoNetSqlHelper.ExecuteRow( @"SELECT * FROM [Locks] L WHERE L.[ObjectType]=@ObjectType AND L.[ObjectID]=@ObjectID AND L.[MainWindowHandle]= ( SELECT [MainWindowHandle] FROM [KeepAlives] K WHERE K.[MainWindowHandle]=L.[MainWindowHandle] AND K.[LastDate]>=@LastDate )", new AdoNetSqlParamCollection( AdoNetSqlParamCollection.CreateParameter( "@ObjectType", objectType.FullName ), AdoNetSqlParamCollection.CreateParameter( "@ObjectID", objectID ), AdoNetSqlParamCollection.CreateParameter( "@LastDate", dt ) ) ); // Last chance, try window handle if on this machine. if ( row!=null ) { string userName; string userWorkstationName; DBHelper.ReadField( out userName, row["UserName"] ); DBHelper.ReadField( out userWorkstationName, row["UserWorkstationName"] ); if ( userWorkstationName==Environment.MachineName ) { int mainWindowHandle; DBHelper.ReadField( out mainWindowHandle, row["MainWindowHandle"] ); if ( IsWindow( new IntPtr( mainWindowHandle ) ) ) { throw new LockException( userName ); } else { LogCentral.Current.LogDebug( string.Format( "Returned row is NON-NULL, window handle is on local machine and does not exist anymore. Not locked for '{0}', '{1}'.", objectType.FullName, objectID ) ); } } else { throw new LockException( userName ); } } else { LogCentral.Current.LogDebug( string.Format( "Returned row is NULL. Not locked for '{0}', '{1}'.", objectType.FullName, objectID ) ); } } // -- AdoNetSqlHelper.ExecuteNonQuery( @"DELETE FROM [Locks] WHERE [ObjectType]=@ObjectType AND [ObjectID]=@ObjectID", new AdoNetSqlParamCollection( AdoNetSqlParamCollection.CreateParameter( "@ObjectType", objectType.FullName ), AdoNetSqlParamCollection.CreateParameter( "@ObjectID", objectID ) ) ); // -- using ( AdoNetSqlUpdater upd = new AdoNetSqlUpdater( @"SELECT * FROM [Locks] WHERE [ObjectType]=@ObjectType AND [ObjectID]=@ObjectID", new AdoNetSqlParamCollection( AdoNetSqlParamCollection.CreateParameter( "@ObjectType", objectType.FullName, DbType.String ), AdoNetSqlParamCollection.CreateParameter( "@ObjectID", objectID, DbType.Int32 ) ) ) ) { // If here, everything is OK. No other lock is set or lock is // invalid. Set new lock now. DataRow row = upd.CheckGetOrAddNewRow(); row["LockDate"] = DBHelper.WriteField( lockDate ); row["UserName"] = DBHelper.WriteField( userName ); row["UserDomainName"] = DBHelper.WriteField( userDomainName ); row["UserWorkstationName"] = DBHelper.WriteField( userWorkstationName ); row["ObjectType"] = DBHelper.WriteField( objectType.FullName ); row["ObjectID"] = DBHelper.WriteField( objectID ); row["MainWindowHandle"] = DBHelper.WriteField( mainWindowHandle ); upd.Update( AdoNetSqlUpdater.IdentityControl.DontGet ); } }