i have the following code:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Const WM_SYSCOMMAND As Integer = &H112
Const SC_SCREENSAVE As Integer = &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 & pausa & vbTab & vbTab & inicio.ToLongTimeString & vbTab & vbTab & fim.ToLongTimeString _
& vbTab & vbTab & diferença.ToString.Substring(0, 8) & vbTab & vbTab & " screensaver" & System.Environment.NewLine
RemoveHandler Application.Idle, AddressOf Application_Idle
End Sub
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.
It's all works fine except for one point:
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)
Now my questions:
Why does it need the messagebox for the now to be updated and why doesn't it work it debug.print?
Is there a way to solve this problem/update the now var, without having to use a messagebox (i wouldn't like for the app to have pop-up messages)
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?
EDIT:
I have been searching and i found this 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) <> 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
i used Timer3.Start() 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?
Doing anything with Application.Idle is a lost cause. Not only does your app go idle immediately after the screen saver activates, you also never stop being idle while it is running. The screen saver switches the active desktop to a dedicated secure desktop, none of the running programs will ever get any input, not until it de-activates.
You can observe the desktop switch, the SystemEvents.SessionSwitch event fires.
Do note the considerable lack of practical usefulness of code like this. Curiosity is okay but there are always a lot of things to learn. The screen saver should be at the bottom of your list.
First i'll thank you guys for the help, like you said application.idle doesn't work, with you help i got this solution i VB:
Imports System
Imports Microsoft.Win32
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
<DllImport("user32.dll", CharSet:=CharSet.Auto)> Public Shared Function SystemParametersInfo(uAction As UInteger, _
uParam As UInteger, ByRef lpvParam As Boolean, fWinIni As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' Check if the screensaver is busy running.'
Public Shared Function IsScreensaverRunning() As Boolean
Const SPI_GETSCREENSAVERRUNNING As Integer = 114
Dim isRunning As Boolean = False
If Not SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, isRunning, 0) Then
' Could not detect screen saver status...'
Return False
End If
If isRunning Then
' Screen saver is ON.'
Return True
End If
' Screen saver is OFF.'
Return False
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Const WM_SYSCOMMAND As Integer = &H112
Const SC_SCREENSAVE As Integer = &HF140
MyBase.WndProc(m)
If bloqueado = 0 Then
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_SCREENSAVE Then
Timer2.Start()
Timer3.Enabled = True
Timer3.Start()
'here we that that the screensaver started running so we start a timer'
End If
End If
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
If IsScreensaverRunning() Then
'Screen Saver Is Running'
Else
Timer3.Stop()
Timer3.Enabled = False
'Screen Saver Is not Running'
End If
End Sub
Because the timer only starts running when the screensaver is running we know that when you get timer3.stop is when the screensaver stopped running
Important, don't put a msgbox before the timer stop because it wont work, the pop-up will show and it wont get to the stop so innumerous pop-up will appear (yeah... i made that mistake :S)
Again, thanks for helping me and hope it will help someone in the future
Related
I'm trying to write an app to perform some basic process automation by sending keyboard events (i.e. simulating single key presses as well as holding keys down) to a window in focus (any window, such as Notepad). I can get single key presses to work just fine, but I can't get it to hold a key down. Even if I do a key down event, followed by a lengthy delay, followed by a key up... all I get is a single keypress.
I've read so many tutorials, and many of them multiple times over to ensure I haven't missed something. Every single time however, all I get is a single key press, it fails to hold the key down.
The following is a code sample I found from:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/bad5b1f3-cf59-4a2b-889b-257ee590bf99/vb-advanced-key-bot?forum=vbgeneral
What I'm expecting to have happen is that it would send a keyboard event that tells the system to hold down a key (e.g. aaaaaaaaaaaaaaaaaaaa), but all I get is a single character. I've tried spamming the system with repeat keypresses, but the receiving app sees the different keyboard code for keydowns and keyups, as opposed to a key in a held status, and thus is not responding as though the key were actually held key down.
What am I doing wrong? Did they maybe change this dll?
A huge thanks to anyone who can help me get this working.
Public Class Form1
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer
' flag to indicate completion
Dim finished As Boolean = True
' how long to 'press' the Space key
Dim delay As Integer = 3
' how many times to repeat Q and Space
Dim Repeats As Integer
' User closes application during processing
Dim UserInterupt As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If Not finished Then
TextBox1.AppendText("USER closing" & vbCrLf)
UserInterupt = True
e.Cancel = True
End If
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
Select Case e.KeyChar
Case "z", "Z"
e.Handled = True
Repeats = 12
finished = False
Do While Not finished
TextBox1.AppendText("Pressing SPACE" & vbCrLf)
HoldKeyDown(Keys.Space, delay)
Loop
Case "x", "X"
e.Handled = True
TextBox1.AppendText("USER stopping" & vbCrLf)
finished = True
End Select
End Sub
Private Sub HoldKeyDown(ByVal k As Keys, ByVal Hold As Integer)
Dim HoldFor As DateTime = DateTime.Now().AddSeconds(Hold)
keybd_event(k, MapVirtualKey(k, 0), 0, 0)
While HoldFor.Subtract(DateTime.Now()).TotalSeconds > 0
Application.DoEvents()
End While
keybd_event(k, MapVirtualKey(k, 0), 2, 0)
TextBox1.AppendText("SPACE released" & vbCrLf)
Repeats -= 1
If Repeats = 0 Then
finished = True
TextBox1.AppendText("REPEATS completed" & vbCrLf)
End If
If UserInterupt Then End
End Sub
End Class
Answering my own question after going right down the rabbit hole on this one.
Basically put, the only way to do this is with SendKeys. The other methods are all deprecated and so will not work in this way anymore.
However this isn't a dead-end for you. If you want to use SendKeys to "hold down" a key, then spam the key at 10ms intervals and this should trigger the receiving app to think the key is held down.
Long time reader, first time poster. Usually I'm able to find the answer and make it work. Not this time..... I'm using VB.NET in VS2013. I am trying to update a progress bar with work done in a secondary thread. Easy right? No. I had to make it more complicated. The progress bar (ToolStripProgressBar1) is on the main form (frmMain), the MDI of the project. A secondary form (frmShipping) has a button which initiates a second thread to do some COMM Port communications in a class (cApex). I can get the progress bar to update on the frmMain from the main UI thread (frmShipping button).
This is the code from button on frmShiping and the multithread procedure:
Private Sub btnreadScanner_Click(sender As Object, e As EventArgs) Handles btnreadScanner.Click
Dim thrReadScanner As New System.Threading.Thread(AddressOf ReadScanner)
thrReadScanner.IsBackground = True
thrReadScanner.Start()
End Sub
Private Sub ReadScanner()
Dim strRowCount As String
ShipmentMsg(2)
strRowCount = objShipping.RecordsExisit.ToString()
Try
objApex.ImmediateMode()
If objApex.FileDownload = False Then
Throw New Exception(Err.Description)
End If
Catch ex As Exception
ShipmentMsg(1)
MessageBox.Show("No Data downloaded from Scanner. Try Again. Error#: " & Err.Number & " : " & Err.Description)
Exit Sub
End Try
RecordCount()
DataGridUpdate()
btnProcessShipment.Enabled = True
ShipmentMsg(5)
ScanErrors()
End Sub
This all works great. As expected. The call to objApex.FileDownload in class cApex is where progress bar is to be updated from (actually in another function called from FileDownload). So here is the code there.
Try
GetHeaderRecord()
If Count <> 0 Then intTicks = Math.Round((100 / Count), 1)
For intcount As Integer = 1 To Count
Dim intLength As Integer = Length
Do While intLength > 0
literal = Chr(_serialPort.ReadChar.ToString)
If literal = ">" Then Exit Do
strRecord = strRecord & literal
intLength = intLength - 1
Loop
REF = strRecord.Substring(0, 16).TrimEnd
SKID = strRecord.Substring(16, 16).TrimEnd
REEL_BC = strRecord.Substring(32, 15).TrimEnd
ScanDate = strRecord.Substring(47, 8).TrimEnd
ScanDate = DateTime.ParseExact(ScanDate, "yyyyMMdd", Nothing).ToString("MM/dd/yyyy")
ScanTime = strRecord.Substring(55, 6).TrimEnd
ScanTime = DateTime.ParseExact(ScanTime, "HHmmss", Nothing).ToString("HH:mm:ss")
strRecordTotal = strRecordTotal & strRecord & CRLF
Dim strSQL As String
strSQL = "INSERT INTO tblScanData (PONo,Barcode,SkidNo,ScanDate,ScanTime) " & _
"VALUES (" & _
Chr(39) & REF & Chr(39) & _
"," & Chr(39) & REEL_BC & Chr(39) & _
"," & Chr(39) & SKID & Chr(39) & _
"," & Chr(39) & ScanDate & Chr(39) & _
"," & Chr(39) & ScanTime & Chr(39) & ")"
objData.Executecommand(strSQL)
strRecord = ""
Next
And finally this is how I was calling the progress bar update.
Dim f As frmMain = frmMain
System.Threading.Thread.Sleep(100)
DirectCast(f, frmMain).ToolStripProgressBar1.PerformStep()
I really need to put the PerformStep in the For loop. Each time around the loop will step the progress bar the percentage of steps needed to make bar fairly accurate (done by the math code before loop). Also I setup the properties of the progress bar control on frmMain. So, am I crazy, or is there a way to accomplish this? I tried using a delegate; Me.Invoke(New MethodInvoker(AddressOf pbStep)) to make code cross thread safe. I don't get an error about cross thread calls, but the progress bar doesn't update either. Sorry it's a long one but I'm lost and my ADHD won't let me scrap this idea.
EDIT AS REQUESTED:
Public Sub pbStep()
Dim f As frmMain = frmMain
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf pbStep))
Else
DirectCast(f, frmMain).ToolStripProgressBar1.PerformStep()
System.Threading.Thread.Sleep(100)
End If
End Sub
Both responses helped lead me to the correct answer I was needing. The code provided by James was a great starting point to build on, and Hans has several post explaining the BackgroundWorker. I wanted to share the "Answer" I came up with. I'm not saying its the best way to do this, and I'm sure I'm violating some rules of common logic. Also, a lot of the code came from a MSDN example and James's code.
Lets start with the form from which I am calling the bgw, frmShipping. I added this code:
Dim WithEvents bgw1 As New System.ComponentModel.BackgroundWorker
Private Sub bgw1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles bgw1.RunWorkerCompleted
If e.Error IsNot Nothing Then
MessageBox.Show("Error: " & e.Error.Message)
ElseIf e.Cancelled Then
MessageBox.Show("Process Canceled.")
Else
MessageBox.Show("Finished Process.")
End If
End Sub
Private Sub bgw1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
Handles bgw1.ProgressChanged
DirectCast(Me.MdiParent, frmMain).ToolStripProgressBar1.Maximum = 1960
DirectCast(Me.MdiParent, frmMain).ToolStripProgressBar1.Step = 2
Dim state As cApex.CurrentState =
CType(e.UserState, cApex.CurrentState)
DirectCast(Me.MdiParent, frmMain).txtCount.Text = state.LinesCounted.ToString
DirectCast(Me.MdiParent, frmMain).txtPercent.Text = e.ProgressPercentage.ToString
DirectCast(Me.MdiParent, frmMain).ToolStripProgressBar1.PerformStep()
End Sub
Private Sub bgw1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles bgw1.DoWork
Dim worker As System.ComponentModel.BackgroundWorker
worker = CType(sender, System.ComponentModel.BackgroundWorker)
Dim objApex As cApex = CType(e.Argument, cApex)
objApex.CountLines(worker, e)
End Sub
Sub StartThread()
Me.txtCount.Text = "0"
Dim objApex As New cApex
bgw1.WorkerReportsProgress = True
bgw1.RunWorkerAsync(objApex)
End Sub
Next I added the following code the my cApex class.
Public Class CurrentState
Public LinesCounted
End Class
Private LinesCounted As Integer = 0
Public Sub CountLines(ByVal worker As System.ComponentModel.BackgroundWorker, _
ByVal e As System.ComponentModel.DoWorkEventArgs)
Dim state As New CurrentState
Dim line = ""
Dim elaspedTime = 20
Dim lastReportDateTime = Now
Dim lineCount = File.ReadAllLines(My.Settings.strGenFilePath).Length
Dim percent = Math.Round(100 / lineCount, 2)
Dim totaldone As Double = 2
Using myStream As New StreamReader(My.Settings.strGenFilePath)
Do While Not myStream.EndOfStream
If worker.CancellationPending Then
e.Cancel = True
Exit Do
Else
line = myStream.ReadLine
LinesCounted += 1
totaldone += percent
If Now > lastReportDateTime.AddMilliseconds(elaspedTime) Then
state.LinesCounted = LinesCounted
worker.ReportProgress(totaldone, state)
lastReportDateTime = Now
End If
System.Threading.Thread.Sleep(2)
End If
Loop
state.LinesCounted = LinesCounted
worker.ReportProgress(totaldone, state)
End Using
End Sub
I also added a couple of text boxes to my main form to show the current line count from the file being read from and the overall progress as a percentage of a 100. Then on the Click event of my button I just call StartThread(). It is not 100% accurate, but its close enough to give the user a very good idea where the process stands. I have a little more work to do to add it to the "ReadScanner" function, where I originally was wanting to use the progress bar. But this function it the longer of the two that I perform on the scanner, writing almost 2,000 lines of code through a COMM Port. I'm happy with the results.
Thank you guys for helping out!
P.S. I have also now added variables to set the pbar.Maximum and the pbar.step since those can change if the scanner file is changed.
Background workers are useful for this purpose. Just use it in combination with a delegate. All the threaded work is done within the DoWork event of the worker. As progress is made, progress is reported within the DoWork event. This in turn fires the ProgressedChanged event of the worker class which is on the same thread as the progressbar. Once the DoWork has completed and is out of scope, the RunWorkerCompleted event is fired. This can be used to do inform the user that the task is complete, etc. Here is a working solution that I threw together. Just paste it behind an empty form and run.
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Threading
Public Class Form1
Private _progressBar As ProgressBar
Private _worker As BackgroundWorker
Sub New()
' This call is required by the designer.
InitializeComponent()
Initialize()
BindComponent()
End Sub
Private Sub Initialize()
_progressBar = New ProgressBar()
_progressBar.Dock = DockStyle.Fill
_worker = New BackgroundWorker()
_worker.WorkerReportsProgress = True
_worker.WorkerSupportsCancellation = True
Me.Controls.Add(_progressBar)
End Sub
Private Sub BindComponent()
AddHandler _worker.ProgressChanged, AddressOf _worker_ProgressChanged
AddHandler _worker.RunWorkerCompleted, AddressOf _worker_RunWorkerCompleted
AddHandler _worker.DoWork, AddressOf _worker_DoWork
AddHandler Me.Load, AddressOf Form1_Load
End Sub
Private Sub Form1_Load()
_worker.RunWorkerAsync()
End Sub
Private Sub _worker_ProgressChanged(ByVal o As Object, ByVal e As ProgressChangedEventArgs)
_progressBar.Increment(e.ProgressPercentage)
End Sub
Private Sub _worker_RunWorkerCompleted(ByVal o As Object, ByVal e As RunWorkerCompletedEventArgs)
End Sub
Private Sub _worker_DoWork(ByVal o As Object, ByVal e As DoWorkEventArgs)
Dim worker = DirectCast(o, BackgroundWorker)
Dim value = 10000
SetProgressMaximum(value)
For x As Integer = 0 To value
Thread.Sleep(100)
worker.ReportProgress(x)
Next
End Sub
Private Sub SetProgressMaximum(ByVal max As Integer)
If _progressBar.InvokeRequired Then
_progressBar.Invoke(Sub() SetProgressMaximum(max))
Else
_progressBar.Maximum = max
End If
End Sub
End Class
In Visual Studio 2008, how do I implement this code?
If MouseButtons() = Windows.Forms.MouseButtons.Left Then
SendKeys.Send("{3}")
SendKeys.SendWait("{1}")
End If
When I press the left button of the mouse, I want it to send 3 & 1 to my Game.
I'll be using this code for a game.
You can get the mouse click with this.
Add a timer "for my code call it DeskClick"
Add a textbox called ShowMouseClick "so you can check"
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
Private Const LBUTTON = &H1
Private Const RBUTTON = &H2
Private Sub DeskClick_Tick(sender As Object, e As EventArgs) Handles DeskClick.Tick
If GetAsyncKeyState(LBUTTON) Then
ShowMouseClick.Text = "Left Click"
ElseIf GetAsyncKeyState(RBUTTON) Then
ShowMouseClick.Text = "Right Click"
Else
ShowMouseClick.Text = ""
End If
End Sub
you also need to turn of PInvokeStackImbalance
Go to Debug then select Exceptions Then MDA and deselect PInvokeStackImbalance
Maybe this is not the best way but it works perfect in my app Whit out any problem
To capture the event and do something
Private Sub ShowMouseClick_TextChanged(sender As Object, e As EventArgs) Handles ShowMouseClick.TextChanged
If ShowMouseClick.Text = "Left Click" Then
TextBox1.Focus()
SendKeys.Send("{3}")
SendKeys.SendWait("{1}")
End If
End Sub
i want that when user is idle for some particular time and mouse doesnot moves on system than it starts counting time from then ownwards and when user moves mouse then time stops and i can save this time in a varianble
You can use the GetLastInputInfo API call.
The following code is mainly from here: http://pinvoke.net/default.aspx/user32/GetLastInputInfo.html
Imports System.Runtime.InteropServices
Public Class Form1
<StructLayout(LayoutKind.Sequential)> _
Structure LASTINPUTINFO
<MarshalAs(UnmanagedType.U4)> _
Public cbSize As Integer
<MarshalAs(UnmanagedType.U4)> _
Public dwTime As Integer
End Structure
<DllImport("user32.dll")> _
Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
End Function
Dim idletime As Integer
Dim lastInputInf As New LASTINPUTINFO()
Public Function GetLastInputTime() As Integer
idletime = 0
lastInputInf.cbSize = Marshal.SizeOf(lastInputInf)
lastInputInf.dwTime = 0
If GetLastInputInfo(lastInputInf) Then
idletime = Environment.TickCount - lastInputInf.dwTime
End If
If idletime > 0 Then
Return idletime / 1000
Else : Return 0
End If
End Function
Private sumofidletime As TimeSpan = New TimeSpan(0)
Private LastLastIdletime As Integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim it As Integer = GetLastInputTime()
If LastLastIdletime > it Then
Label1.Text = "IDLE STATE CHANGED!"
sumofidletime = sumofidletime.Add(TimeSpan.FromSeconds(LastLastIdletime))
Label2.Text = "Sum of idle time: " & sumofidletime.ToString
Else
Label1.Text = GetLastInputTime()
End If
LastLastIdletime = it
End Sub
End Class
This code displays the seconds the user has been idle since the last input action in the label on every timer tick. It also checks if the idle state has changed. So at this point you can react to it and save the LastLastIdletime as the amount of time in seconds that the user was inactive.
To prevent having to handle this for many controls you can rearrange things a bit and cache the information needed to know if cursor has moved and how long the idle time is, to do this you need a Point variable and a Date variable. The Timer needs to tick all the time. In addition, to balance the cursor Show/Hide calls you need a variable to keep track of its visibility state. Here is the complete code sample:
Private loc As Point, idle As Date, hidden As Boolean
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If loc <> Cursor.Position Then
If hidden Then
Cursor.Show()
hidden = False
End If
loc = Cursor.Position
idle = Date.Now
ElseIf Not hidden AndAlso (Date.Now - idle).TotalSeconds > 3 Then
Cursor.Hide()
hidden = True
End If
End Sub
This Timer can tick each 1/2-1 seconds depending on how responsive you want it, the idle time is set to 3 seconds. The code should be easy to understand when you read it and give it some thought, if not ask
frmMain has a button that kicks off a background worker
Private Sub btnProcessITN_Click(sender As Object, e As EventArgs) Handles btnProcessITN.Click
Me.frmMainProgressBar.Visible = True
Me.btnProcessITN.Text = "working..." & Me.frmMainProgressBar.Value & "%"
Me.btnProcessITN.Enabled = False
backgroundWorker2.WorkerReportsProgress = True
BackgroundWorker2.RunWorkerAsync()
End Sub
Private Sub backgroundWorker2_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
ProcessITN()
End Sub
ProcessITN lives in modITN. This runs, does its thing, then PROBLEM
Public Sub ProcessITN()
...doing work here....
frmMain.updateProgress(current, Max)
End Sub
and back in form main I have a function that tries to update the progress of the background worker, and update a progress bar on the form
Public Function updateProgress(current As Integer, Max As Integer) As Boolean
BackgroundWorker2.ReportProgress((current / Max) * 100)
Return True
End Function
Private Sub backgroundWorker2_ProgressChanged( _
ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) _ Handles BackgroundWorker2.ProgressChanged
Me.frmMainProgressBar.Value = e.ProgressPercentage
Me.btnProcessITN.Text = "working..." & Me.frmMainProgressBar.Value & "%"
'Me.txtProgress.Text = Me.txtProgress.Text & vbNewLine & strMessage
Me.txtProgress.AppendText(vbNewLine & strMessage)
End Sub
Now, the problem is, that although the code is running, and the values are being passed across correctly from modITN to frmMain, the form itself is not refreshing. the progress bar does not change. txtProgress does not change.
N.B this code was working perfectly when the stuff within modITN was within frmMain. I moved it to try and tidy my code.
You may still have a look here, I had the same question - and there are some more issues to it.
Multithreading for a progressbar and code locations (vb.net)?
VB.net's 'Default Instance' of Forms is a hideous trap in my opinion.
I've sorted the problem!
I moved the backgroundworker blocks to modITN, out frmMain. The whole thing runs in modITN now, and updates the progress bar from there. frmMain simply calls the sub in modITN to start the whole thing off.
Simple!