Timer and Windows XP Logoff Problems

linum

Active Member
Licensed User
Hello. I have a real simple app that runs hidden (as a process) and every three minutes it connects to a MySQL database and performs some functions depending on the MySQL database results.

I use a Timer to accomplish this but the issue now is that many users are not able to logoff from their computers. If I kill my app from the process list then they will be able to logoff with no problems.

From the testing and troubleshooting I've done everything points to the timer running non-stop. I don't know how to get around this problem. Here's my code:

B4X:
Sub Globals
   'Declare the global variables here.
   
   Dim SQLUser(0) As string
   ThisUser = ""
   Seconds = 0
   

End Sub

Sub App_Start
   'ErrorLabel(GottoSt)   
   
   'thr.New1(B4PObject(1))
   Connection.New1
   Reader.New1
   Command.New1("", Connection.Value)   
   
   obj.New1(False)   
   obj.CreateNew("System.Environment" & obj.System_Mscorlib)
   slUs = obj.RunMethod2("GetFolderPath", "LocalApplicationData", "System.Environment+SpecialFolder")
   SQLUser() = StrSplit(slUs, "\")
   uSpot = ArrayLen(SQLUser())-3 
   ThisUser = SQLUser(uSpot)
   ThisUser = StrToLower(ThisUser)
   
   obj.FromControl("frmMain")
   
   frmMain.Show
   Timer1.Enabled = True   
   Timer2.Enabled = True
   
   Return
   
   'GottoSt:   
   
End Sub

Sub btnHide_Click
   
   obj.SetProperty("Visible",False)
   
End Sub

Sub Timer1_Tick
   
   btnHide_Click
   Timer1.Enabled = False
   CheckForNewOnes_Do
   'StartProcess_Do
   
End Sub

Sub frmMain_Close
   
   btnHide_Click
   frmMain.CancelClose
   
End Sub

Sub Timer2_Tick
   
   'Sleep(180)
   'thr.Sleep(2000)
   
   'CheckForNewOnes_Do
   
   
   Seconds = Seconds + 1
   If Seconds = 180 Then '300
      CheckForNewOnes_Do
      Seconds = 0
   End If
   
   DoEvents
   
End Sub

Sub CheckForNewOnes_Do
   ErrorLabel(GottoEx)
   
   Detttp = ""
   Tttype = ""
   
   Connection.Open("DSN=SupACCTdb;")
   Connection.BeginTransaction(Command.Value)
   Command.CommandText = "SELECT * FROM t_eMailsMngr"
   Command.ExecuteTable("tbleMail", 0)
   Connection.EndTransaction
   Connection.Close
   
   For i=0 To tbleMail.RowCount-1
      verifier = tbleMail.Cell("email", i)
      verifier = StrToLower(verifier)
      If verifier = ThisUser Then
         Detttp = tbleMail.Cell("Dept", i)
         If Detttp <> "" Then
            Tttype = "M"
         End If
         If Detttp = "Admin" Then
            Tttype = "X"
         End If
         If Detttp = "NURSE" Then
            Tttype = "N"
         End If   
      End If
   Next
   
   If Detttp = "" Then
      Connection.Open("DSN=SupACCTdb;")
      Connection.BeginTransaction(Command.Value)
      Command.CommandText = "SELECT * FROM t_eMailsSups"
      Command.ExecuteTable("tbleMail", 0)
      Connection.EndTransaction
      Connection.Close
      
      For i=0 To tbleMail.RowCount-1
         verifier = tbleMail.Cell("email", i)
         verifier = StrToLower(verifier)
         If verifier = ThisUser Then
            Detttp = tbleMail.Cell("Dept", i)
            Tttype = "S"
         End If
      Next
   End If
   
   If Detttp = "" Then
      Return
   End If
   
   Connection.Open("DSN=SupACCTdb;")
   Connection.BeginTransaction(Command.Value)
   Command.CommandText = "SELECT * FROM t_IncAccI"
   Command.ExecuteTable("tblSQL", 0)
   Connection.EndTransaction
   Connection.Close
   lblRv.Text = 0
   lblRjc.Text = 0
   
   If Tttype = "M" Then
      tblSQL.Filter("")
      tblSQL.Filter("Sec1Dept = '" & Detttp & "' AND FORMStatus = 'ToManager'")
      lblRv.Text = tblSQL.RowCount
      
      tblSQL.Filter("")
      tblSQL.Filter("Sec1Dept = '" & Detttp & "' AND FORMStatus = 'RejectedToManager'")
      lblRjc.Text = tblSQL.RowCount
   End If   
   If Tttype = "S" Then      
      lblRv.Text = 0
      
      tblSQL.Filter("")
      tblSQL.Filter("Sec1Dept = '" & Detttp & "' AND FORMStatus = 'Rejected'")
      lblRjc.Text = tblSQL.RowCount
   End If
   If Tttype = "X" Then
      tblSQL.Filter("")
      tblSQL.Filter("FORMStatus = 'ToSafety'")
      lblRv.Text = tblSQL.RowCount
      
      lblRjc.Text = 0
   End If
   If Tttype = "N" Then
      tblSQL.Filter("")
      tblSQL.Filter("FORMStatus = 'AcceptedToNurse'")
      lblRv.Text = tblSQL.RowCount
      
      lblRjc.Text = 0
   End If
   
   If lblRv.Text > 0 OR lblRjc.Text > 0 Then
      obj.SetProperty("Visible",True)
      lblDpt.Text = "Dept: " & Detttp
   End If
   
   'StartProcess_Do
   Return
   
   GottoEx:
   'StartProcess_Do

End Sub
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Hi linum,

I had exactly the same problem with an app that I wrote some time ago.
In this app I used the Form.CancelClose to ensure that my app ran hidden in the background much the same as you are doing but this has the adverse affect of preventing Windows from shutting down correctly.
To get around the problem I used DZEventsMagicDesktop.dll to capture the windows messages that are sent when the system is requesting to shut down.

In AppStart add the following lines (AllowToClose needs to be added as a global)....
B4X:
 WinMessage.New1( frmMain.ControlRef , True )
 WinMessage.Hook( 17 )
 WinMessage1.New1( frmMain.ControlRef , False )
 WinMessage1.Hook( 22 )
 AllowToClose = False

Then create two new Subs that will force the Form to close....
B4X:
Sub WinMessage_MagicEvent
 AllowToClose = True
 frmMain.Close
End Sub

Sub WinMessage1_MagicEvent
 AllowToClose = True
 frmMain.Close
End Sub

Then finally in the Form_Close sub do this....
B4X:
Sub frmMain_Close
 If AllowToClose = False Then
  btnHide_Click
  frmMain.CancelClose
 Else
  WinMessage.UnHook( 17 )
  WinMessage1.UnHook( 22 )   
End Sub

Hopefully I've copied this across correctly without introducing any typo's. You will also need to add DZEventsMagicDesktop.dll in the components.

With a little luck this should solve your problem.

Regards,
RandomCoder
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…