Note that there are some explanatory texts on larger screens.

plurals
  1. POEventhandler "bug" using VB.NET with windows forms
    text
    copied!<p>i have the following code:</p> <pre><code>Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) Const WM_SYSCOMMAND As Integer = &amp;H112 Const SC_SCREENSAVE As Integer = &amp;HF140 MyBase.WndProc(m) If bloqueado = 0 Then If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_SCREENSAVE Then Timer2.Start() inicio = Now pausa = pausa + 1 AddHandler Application.Idle, AddressOf Application_Idle End If End If End Sub Private Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs) Dim newitem As ListViewItem Dim diferença As TimeSpan 'MsgBox(Now.ToString)' Debug.Print(Now.ToString) fim = Now diferença = fim - inicio Timer2.Stop() newitem = New ListViewItem newitem.Text = pausa newitem.SubItems.Add(inicio.ToLongTimeString) newitem.SubItems.Add(fim.ToLongTimeString) newitem.SubItems.Add(diferença.ToString.Substring(0, 8)) ListView1.Items.Add(newitem) parcial = parcial &amp; pausa &amp; vbTab &amp; vbTab &amp; inicio.ToLongTimeString &amp; vbTab &amp; vbTab &amp; fim.ToLongTimeString _ &amp; vbTab &amp; vbTab &amp; diferença.ToString.Substring(0, 8) &amp; vbTab &amp; vbTab &amp; " screensaver" &amp; System.Environment.NewLine RemoveHandler Application.Idle, AddressOf Application_Idle End Sub </code></pre> <p>Basically the first part detect when screensaver activates and creates a application.idle event handler and the second part, when activity is detected a bunch of code is run and the handler removed.</p> <p>It's all works fine except for one point:</p> <p>As you can see i have inicio = now when screensaver becomes active and fim = now when activity is detected (when screensaver becomes inactive), so i should have 2 differente times, but if i have it like i posted the 2 datetime will be the same. If you notice i have a msgbox displaying the now (when screensaver stops) in comment, if i take it out of comment the 2 datetimes will be differente and correct (i used a cronometer to make sure of the results)</p> <p>Now my questions: Why does it need the messagebox for the <code>now</code> to be updated and why doesn't it work it debug.print?</p> <p>Is there a way to solve this problem/update the <code>now</code> var, without having to use a messagebox (i wouldn't like for the app to have pop-up messages)</p> <p>If i really have to use msgbox for this purpose is there a way for it not to send the pop-up or to autoclick ok right after so it disappears instantly?</p> <p>EDIT:</p> <p>I have been searching and i found this code:</p> <pre><code>Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Public Function IsSNRunning() As Boolean IsSNRunning = (FindWindow("WindowsScreenSaverClass", vbNullString) &lt;&gt; 0) End Function Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick If IsSNRunning() Then 'Screen Saver Is Running Else Timer3.Stop() code End If End Sub </code></pre> <p>i used <code>Timer3.Start()</code> when in the part that captures the start of the screensaver, my idea being if i start the timer when i know the screensaver if on, then when i get IsSNRunning as false is when the screensaver stops running, but it doesn't work, any ideas why?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload