I'm writing this program in Visual Basic .NET to organize different fields of data, and I'm using profile slots through application settings to store the data for users. But, I ran into a stack overflow error in my SlotSelect.vb class. My best plausible guess for why this happens is that I'm using the wrong kind of variable container in the below sauce code, but my dilemma is that I don't know what specifically is going wrong.
The code that the vshost is saying is the cause for the overflow was written from some code that I looked up on MSDN and other places for referring to objects in other classes, and I tried using other variants of it to see if it was any different. So far, nothing has worked, and it doesn't stop the error while compiling, much less in the code markup--it only catches it when it starts the application in debug after it finishes building.
Here's the sauce code for SlotSelect.vb. Since most of the other unrelated classes (and this one) lead into MainForm.vb, I'll include its sauce too. The location vshost gave as the error is on the line where variable _MainForm is defined for Flaglister.MainForm to be used within SlotSelect.vb.
SlotSelect.vb
Public Class SlotSelect
' Class variables
Private _MainForm As Flaglister.MainForm = New Flaglister.MainForm
Private _SaveSlot As Flaglister.SaveSlot = New Flaglister.SaveSlot
Private _Misc As Flaglister.Misc = New Flaglister.Misc
Private _FlagsTextBox As System.Windows.Forms.TextBox = Flaglister.MainForm.FlagsTextBox
Private _VarsTextBox As System.Windows.Forms.TextBox = Flaglister.MainForm.VarsTextBox
Private _HackNameTextBox As System.Windows.Forms.TextBox = Flaglister.MainForm.HackNameTextBox
Private _RomCodeTextBox As System.Windows.Forms.TextBox = Flaglister.MainForm.RomCodeTextBox
Private _NotesTextBox As System.Windows.Forms.TextBox = Flaglister.MainForm.NotesTextBox
Private _ExpandedCheckBox As System.Windows.Forms.CheckBox = Flaglister.MainForm.ExpandedCheckBox
' Slot selection main execution subs
Friend Sub _0()
Try
' Disable FlagsTextBox
_FlagsTextBox.DeselectAll()
_FlagsTextBox.ClearUndo()
_FlagsTextBox.Clear()
_FlagsTextBox.Enabled = False
' Disable VarsTextBox
_VarsTextBox.DeselectAll()
_VarsTextBox.ClearUndo()
_VarsTextBox.Clear()
_VarsTextBox.Enabled = False
' Disable HackNameTextBox
_HackNameTextBox.DeselectAll()
_HackNameTextBox.ClearUndo()
_HackNameTextBox.Clear()
_HackNameTextBox.Enabled = False
' Disable RomCodeTextBox
_RomCodeTextBox.DeselectAll()
_RomCodeTextBox.ClearUndo()
_RomCodeTextBox.Clear()
_RomCodeTextBox.Enabled = False
' Disable NotesTextBox
_NotesTextBox.DeselectAll()
_NotesTextBox.ClearUndo()
_NotesTextBox.Clear()
_NotesTextBox.Enabled = False
Catch
Call _Misc.ErrorClose()
End Try
End Sub
Friend Sub _1()
Try
' Load flaglist
_FlagsTextBox.DeselectAll()
_FlagsTextBox.ClearUndo()
_FlagsTextBox.Clear()
_FlagsTextBox.Enabled = True
_FlagsTextBox.Text = My.Settings.Flaglist_1
' Load varlist
_VarsTextBox.DeselectAll()
_VarsTextBox.ClearUndo()
_VarsTextBox.Clear()
_VarsTextBox.Enabled = True
_VarsTextBox.Text = My.Settings.Varlist_1
' Load project name
_HackNameTextBox.DeselectAll()
_HackNameTextBox.ClearUndo()
_HackNameTextBox.Clear()
_HackNameTextBox.Enabled = True
_HackNameTextBox.Text = My.Settings.Hackname_1
' Load ROM codename
_RomCodeTextBox.DeselectAll()
_RomCodeTextBox.ClearUndo()
_RomCodeTextBox.Clear()
_RomCodeTextBox.Enabled = True
_RomCodeTextBox.Text = My.Settings.Romcode_1
' Load other notes
_NotesTextBox.DeselectAll()
_NotesTextBox.ClearUndo()
_NotesTextBox.Clear()
_NotesTextBox.Enabled = True
_NotesTextBox.Text = My.Settings.Notes_1
Catch
Call _Misc.ErrorClose()
End Try
End Sub
Friend Sub _2()
Try
' Load flaglist
_FlagsTextBox.DeselectAll()
_FlagsTextBox.ClearUndo()
_FlagsTextBox.Clear()
_FlagsTextBox.Enabled = True
_FlagsTextBox.Text = My.Settings.Flaglist_2
' Load varlist
_VarsTextBox.DeselectAll()
_VarsTextBox.ClearUndo()
_VarsTextBox.Clear()
_VarsTextBox.Enabled = True
_VarsTextBox.Text = My.Settings.Varlist_2
' Load project name
_HackNameTextBox.DeselectAll()
_HackNameTextBox.ClearUndo()
_HackNameTextBox.Clear()
_HackNameTextBox.Enabled = True
_HackNameTextBox.Text = My.Settings.Hackname_2
' Load ROM codename
_RomCodeTextBox.DeselectAll()
_RomCodeTextBox.ClearUndo()
_RomCodeTextBox.Clear()
_RomCodeTextBox.Enabled = True
_RomCodeTextBox.Text = My.Settings.Romcode_2
' Load other notes
_NotesTextBox.DeselectAll()
_NotesTextBox.ClearUndo()
_NotesTextBox.Clear()
_NotesTextBox.Enabled = True
_NotesTextBox.Text = My.Settings.Notes_2
Catch
Call _Misc.ErrorClose()
End Try
End Sub
Friend Sub _3()
Try
' Load flaglist
_FlagsTextBox.DeselectAll()
_FlagsTextBox.ClearUndo()
_FlagsTextBox.Clear()
_FlagsTextBox.Enabled = True
_FlagsTextBox.Text = My.Settings.Flaglist_3
' Load varlist
_VarsTextBox.DeselectAll()
_VarsTextBox.ClearUndo()
_VarsTextBox.Clear()
_VarsTextBox.Enabled = True
_VarsTextBox.Text = My.Settings.Varlist_3
' Load project name
_HackNameTextBox.DeselectAll()
_HackNameTextBox.ClearUndo()
_HackNameTextBox.Clear()
_HackNameTextBox.Enabled = True
_HackNameTextBox.Text = My.Settings.Hackname_3
' Load ROM codename
_RomCodeTextBox.DeselectAll()
_RomCodeTextBox.ClearUndo()
_RomCodeTextBox.Clear()
_RomCodeTextBox.Enabled = True
_RomCodeTextBox.Text = My.Settings.Romcode_3
' Load other notes
_NotesTextBox.DeselectAll()
_NotesTextBox.ClearUndo()
_NotesTextBox.Clear()
_NotesTextBox.Enabled = True
_NotesTextBox.Text = My.Settings.Notes_3
Catch
Call _Misc.ErrorClose()
End Try
End Sub
' Save all slots
Friend Sub SlotSaveAll()
Call _SaveSlot.FlaglistSave()
Call _SaveSlot.VarlistSave()
Call _SaveSlot.HacknameSave()
Call _SaveSlot.RomcodeSave()
Call _SaveSlot.NotesSave()
Call _SaveSlot.ExpandedSave()
End Sub
End Class
MainForm.vb
Public Class MainForm
' Class-level variables
Private _SlotSelect As New Flaglister.SlotSelect
Private _SaveSlot As New Flaglister.SaveSlot
Private _Misc As New Flaglister.Misc
' Startup/Shutdown events
Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Slot0RadioButton.Checked = True Then
Call _SlotSelect._0()
End If
End Sub
Sub MainForm_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
My.Settings.Save()
Me.Close()
End Sub
' Form object events
Private Sub Slot0RadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Slot0RadioButton.CheckedChanged
If Slot0RadioButton.Checked = True Then
Call _SlotSelect._0()
End If
End Sub
Private Sub Slot1RadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Slot1RadioButton.CheckedChanged
If Slot1RadioButton.Checked = True Then
Call _SlotSelect._1()
End If
End Sub
Private Sub Slot2RadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Slot2RadioButton.CheckedChanged
If Slot2RadioButton.Checked = True Then
Call _SlotSelect._2()
End If
End Sub
Private Sub Slot3RadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Slot3RadioButton.CheckedChanged
If Slot3RadioButton.Checked = True Then
Call _SlotSelect._3()
End If
End Sub
Private Sub FlagsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlagsTextBox.TextChanged
Call _SaveSlot.FlaglistSave()
End Sub
Private Sub VarsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VarsTextBox.TextChanged
Call _SaveSlot.VarlistSave()
End Sub
Private Sub HackNameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HackNameTextBox.TextChanged
Call _SaveSlot.HacknameSave()
End Sub
Private Sub RomCodeTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RomCodeTextBox.TextChanged
Call _SaveSlot.RomcodeSave()
End Sub
Private Sub NotesTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NotesTextBox.TextChanged
Call _SaveSlot.NotesSave()
End Sub
Private Sub ExpandedCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExpandedCheckBox.CheckedChanged
Call _SaveSlot.ExpandedSave()
End Sub
End Class
What's the problem in the code?
Here's some additional details about me and my program.
Computer
System: Seven x64 w/ AMD Sempron
Compiler: Microsoft Visual Basic 2010 Express
Application
Framework: Microsoft .NET version 3.5
Root Namespace: Flaglister
Assembly name: Flaglister
Application Type: Windows Forms Application
Sometimes I think too much, so I'm sorry if the answer is obvious to many and I don't see it.
EDIT
Here's a screenshot of the callstack at its current position when the error is thrown, and another screenshot of the popup error box. I would've never guessed to add these.
The issue here is recursive initialization. MainForm contains:
Private _SlotSelect As New Flaglister.SlotSelect
And SlotSelect contains:
Private _MainForm As Flaglister.MainForm = New Flaglister.MainForm
They keep calling each other's constructor, causing your stack overflow. You can fix this by passing in _MainForm as an argument in SlotSelect's constructor. Like so:
Public Class SlotSelect
' Class variables
Private _MainForm As Flaglister.MainForm
' (truncated for space)
Public Sub New(ByVal mainForm As Flaglister.MainForm)
Me._MainForm = mainForm
End Sub
'...
And in MainForm.vb, pass SlotSelect's constructor the current instance:
Private _SlotSelect As New Flaglister.SlotSelect(Me)
I'm guessing it is because in your class you are declaring and instantiating a new instance of MainForm
Private _MainForm As Flaglister.MainForm = New Flaglister.MainForm
Then in the MainForm class you are declaring and instantiating a new instance of the SlotSelect class
Private _SlotSelect As New Flaglister.SlotSelect
So the two classes just "bounce" back and forth creating new instances of each other, eventually giving you the stack overflow.
You are in an infinite loop.
In slotselect you have
Private _MainForm As Flaglister.MainForm = New Flaglister.MainForm
which creates a new main form, and then in MainForm you have
Private _SlotSelect As New Flaglister.SlotSelect
which creates a new slotselect.
Make one object the parent and one the child, and hand the parent reference in the constructor e.g.
Class SlotSelect
Private _MainForm As MainForm
Sub New(mf as MainForm)
_MainForm=mf
End Sub
...
End Class
Related
I have 2 forms, MainForm and RCONForm.
What I am trying to do is to start a process when the programs starts and in this case I have choosen cmd.exe and it works.
The thing is I want to be able to read the output and send input onto the process from another form using 2 textboxes.
The problem is that I can't read anything from the process neither can I send any input to it either.
My MainForm:
Public Class MainForm
#Region "Import Function"
Dim Functions As New Functions
Friend WithEvents RCON As Process
#End Region
Friend Sub AppendOutputText(ByVal text As String)
If RCONForm.RCONLogText.InvokeRequired Then
Dim myDelegate As New RCONForm.AppendOutputTextDelegate(AddressOf AppendOutputText)
Me.Invoke(myDelegate, text)
Else
RCONForm.RCONLogText.AppendText(text)
End If
End Sub
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
RCON = New Process
With RCON.StartInfo
.FileName = "C:\Windows\system32\CMD.exe"
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
End With
RCON.Start()
RCON.BeginErrorReadLine()
RCON.BeginOutputReadLine()
AppendOutputText("RCON Started at: " & RCON.StartTime.ToString)
End Sub
Private Sub RCONButton_Click(sender As Object, e As EventArgs) Handles RCONButton.Click
Functions.KillOldForm()
Functions.SpawnForm(Of RCONForm)()
End Sub
Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
Application.Exit()
End Sub
Private Sub ServerButton_Click(sender As Object, e As EventArgs) Handles ServerButton.Click
Functions.KillOldForm()
Functions.SpawnForm(Of ServerForm)()
End Sub
End Class
And my RCONForm:
Public Class RCONForm
Friend Delegate Sub AppendOutputTextDelegate(ByVal text As String)
Friend WithEvents RCON As Process = MainForm.RCON
Friend Sub RCON_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles RCON.ErrorDataReceived
MainForm.AppendOutputText(vbCrLf & "Error: " & e.Data)
End Sub
Friend Sub RCON_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles RCON.OutputDataReceived
MainForm.AppendOutputText(vbCrLf & e.Data)
End Sub
Friend Sub RCONCommandText_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RCONCommandText.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
MainForm.RCON.StandardInput.WriteLine(RCONCommandText.Text)
MainForm.RCON.StandardInput.Flush()
RCONCommandText.Text = ""
e.Handled = True
End If
End Sub
Friend Sub RCONForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
MainForm.RCON.StandardInput.WriteLine("EXIT") 'send an EXIT command to the Command Prompt
MainForm.RCON.StandardInput.Flush()
MainForm.RCON.Close()
End Sub
Private Sub RCONForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
My Functions:
#Region "Kill Old Forms"
Function KillOldForm()
While MainForm.SpawnPanel.Controls.Count > 0
MainForm.SpawnPanel.Controls(0).Dispose()
End While
Return Nothing
End Function
#End Region
#Region "Spawn Form"
Function SpawnForm(Of T As {New, Form})() As T
Dim spawn As New T() With {.TopLevel = False, .AutoSize = False}
Try
spawn.Dock = DockStyle.Fill
MainForm.SpawnPanel.Controls.Add(spawn)
spawn.Show()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return Nothing
End Function
#End Region
I have been thinking of using threads and maybe that can solve the issue, or is there a more simple way?
You are using default form instances heavily. You should avoid that if possible. Here's a way to keep track of the proper instance of RCONForm in MainForm
Private myRCONForm As RCONForm
Private Sub RCONButton_Click(sender As Object, e As EventArgs) Handles RCONButton.Click
Functions.KillOldForm()
myRCONForm = Functions.SpawnForm(Of RCONForm)()
End Sub
Now SpawnForm is a function, and it would return the form so you can keep a reference to it
Public Function SpawnForm(Of T As {New, Form})() As T
Dim myForm = New T()
' add myForm to the appropriate Panel or whatever
Return myForm
End Function
Update all access to RCONForm with myRCONForm in MainForm
Also, this is a little flawed, where you check if invocation is required on RCONForm.RCONLogText, then invoke on Me. It can be simplified
' old with default form instance
Friend Sub AppendOutputText(ByVal text As String)
If RCONForm.RCONLogText.InvokeRequired Then
Dim myDelegate As New RCONForm.AppendOutputTextDelegate(AddressOf AppendOutputText)
Me.Invoke(myDelegate, text)
Else
RCONForm.RCONLogText.AppendText(text)
End If
End Sub
' new, with instance reference plus simplified invocation
Friend Sub AppendOutputText(text As String)
If myRCONForm.RCONLogText.InvokeRequired Then
myRCONForm.RCONLogText.Invoke(New Action(Of String)(AddressOf AppendOutputText), text)
Else
myRCONForm.RCONLogText.AppendText(text)
End If
End Sub
I have two radio buttons in form2 and if one of them is checked, I want to display information in a label in form4. please help
Here is what I have :
Public Class Form4
Public Form2 As New Object
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Me.Form2.RadioButton1 = CheckState.Checked Then
Label2.Text = "You have been successfully registered for the following modules in the first semester."
Else
Label2.Text = "You have been successfully registered for the following modules in the second semester."
End If
Here is an easy way:-
Form 2
Public Class Form2
Dim check As Integer = 0
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
check = 1
Else
check = 0
End If
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton1.Checked = True Then
check = 2
Else
check = 0
End If
End Sub
Public Function sendCheck() As Integer
return check
End Function
End Class
Form 4
Public Class Form4
Dim receivedCheck As Integer = Form2.sendCheck
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If receivedCheck = 1 Then
Label2.Text = "You have been successfully registered for the following modules in the first semester."
ElseIf receivedCheck = 2 Then
Label2.Text = "You have been successfully registered for the following modules in the second semester."
End If
End Sub
End Class
Explanation
The error you may be getting in is the condition in If statement. What you have done is, you have used Me ahead of Form2. Me is used to refer objects within the form and for referring to other forms we use their name.
Remember, a form can never call itself with its name. It will call itself by Me. But here, you have used both Me and Form2 (Which means Form2 is an object within Form4) which we don't have to do.
So, remove the two letters Me and your program will work fine.
But, it would be better if you generate a checkedchanged event. This is how I would have written it, but you can write anyways you want, just the main problem that you may be getting was the use of Me and Form2 both.
This is how I would have written it:
Code And Example
Public Class Form2
Private Sub CheckedChanged () Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
If Form2.RadioButton1.Checked = True Then
Label2.Text = "You have been successfully registered for the following modules in the first semester."
Else
Label2.Text = "You have been successfully registered for the following modules in the second semester."
End If
End Sub
End Class
I hope that you will understand!
I would like to know how I should code my VB.net application to react to the form load event in a third-party application (also written in VB.net)
To test, I have created two basic programs, one with two forms (program A) and one (program B) that attempts to the listen to program A's appropriate form load event. I have tried using WithEvents but it does not get fired when Program's A second form loads.
Here is the code for Program A:
Public Class StartPage
Public WithEvents loadtimer As New System.Windows.Forms.Timer
Private Sub StartPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
loadtimer.Interval = 1000
loadtimer.Enabled = True
loadtimer.Start()
End Sub
Private Sub loadtimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles loadtimer.Tick
loadtimer.Stop()
SystemStatus.Show()
End Sub
End Class
Public Class SystemStatus
Inherits StartPage
Private Sub StartPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Label1.Text = "This is the form that I want to listen for the load event"
Me.loadtimer.Enabled = False
End Sub
End Class
Here is the code for Program B:
Imports Program_A
Public Class ListeningForm
Dim WithEvents testlisten As New Program_A.SystemStatus
Private Sub testlisten_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles testlisten.Load
Label1.Text = "SystemStatus form loaded"
End Sub
Private Sub ListeningForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "Waiting for SystemStatus load event..."
End Sub
End Class
I am quite new when it comes to programming so maybe this is not even possible or I just haven't been understanding what I've been reading. In any case please enlighten me as to what my next course of action should be.
Thanks very much in advance,
theoleric
This will do what your asking.
Imports Program_A
Public Class ListeningForm
Dim WithEvents testlisten As New SystemStatus
Private Sub testlisten_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles testlisten.Load
' now this event will fire
Label1.Text = "SystemStatus form loaded"
End Sub
Private Sub ListeningForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "Waiting for SystemStatus load event..."
' the load event will not fire until you call this
testlisten.Show()
End Sub
End Class
why does the FileSystemWatcher fire twice? Is there an easy way to fix it? Surely if I update or edit the text file it should only fire once?
this link here http://weblogs.asp.net/ashben/archive/2003/10/14/31773.aspx says
Events being raised twice - An event will be raised twice if an event handler (AddHander FSW.Created, AddressOf FSW_Created) is
explicitly specified. This is because, by default, the public events
automatically call the respective protected methods (OnChanged,
OnCreated, OnDeleted, OnRenamed). To correct this problem, simply
remove the explicit event handler (AddHandler ...).
What does "remove the explicit event handler" mean?
Imports System.IO
Public Class Form2
Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
'this fires twice
MessageBox.Show("test")
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FileSystemWatcher1.Path = "C:\Users\c\Desktop\test\"
FileSystemWatcher1.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.CreationTime
FileSystemWatcher1.IncludeSubdirectories = False
FileSystemWatcher1.Filter = "text.txt"
End Sub
End Class
Update:
I have come up with 2 solutions. One uses Threads, and the other doesn't. Take your pick :-).
Without threading:
Imports System.IO
Public Class Form1
Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
Dim watcher As System.IO.FileSystemWatcher = sender
watcher.EnableRaisingEvents = False
'Do work here while new events are not being raised.
MessageBox.Show("Test")
watcher.EnableRaisingEvents = True 'Now we can begin watching for new events.
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FileSystemWatcher1.Path = "C:\Users\c\Desktop\test"
FileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite
FileSystemWatcher1.IncludeSubdirectories = False
FileSystemWatcher1.Filter = "test.txt"
End Sub
Private Sub FileSystemWatcher_OnChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
This solution (without threading), sets the watcher.EnableRaisingEvents to False. It is after this point where you would normally process whatever files are affected (or changed). It then sets the EnableRaisingEvents back to True after your work is done.
With threading:
Imports System.IO
Public Class Form1
Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
FileSystemWatcher1.EnableRaisingEvents = False
Threading.Thread.Sleep(250)
FileSystemWatcher1.EnableRaisingEvents = True
MessageBox.Show("test")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FileSystemWatcher1.Path = "C:\Users\c\Desktop\test"
FileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite
FileSystemWatcher1.IncludeSubdirectories = False
FileSystemWatcher1.Filter = "test.txt"
End Sub
Private Sub FileSystemWatcher_OnChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
This solution, although a bit hacky, does work. It disables checking for new changes/events for 250ms and then re-enables checking, based on the assumption that you won't been needing to check for a change every 250ms. I have tried almost everything that I could think of to get a real solution for you but this will work in the meantime.
Check e.ChangeType. I imagine you're getting two different notifications. Perhaps LastAccess and LastModified. In which case, that's the expected behavior.
Today i crashed in FileSystemWatcher and found this site. Suggested Thread.Sleep cannot completely eliminate problem. Tested with fast counter-integer. And is blocking UI. Most problematic is startup, it sliped trough at 5s. Then i set FileSystemWatcher1.EnableRaisingEvents = False immediately in TimerWatcherChanged.Tick and never enabled again... But surprisingly counter still catch up-to 4 events! I would like to share my solution, non-blocking, with adjustable Timer. Feedback is welcome.
Imports System.IO
Imports System.Diagnostics
Public Class Form1
Dim fileName As String
Dim Fsw_counter As Integer
WithEvents TimerWatcherChanged As New Windows.Forms.Timer
WithEvents TimerTest As New Windows.Forms.Timer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TimerWatcherChanged.Interval = 100
TimerTest.Interval = 100 : TimerTest.Start()
TextBox1.Text = "C:\Downloads\New Text Document.txt"
TextBox1.SelectionStart = TextBox1.Text.Length
WatcherSetup()
End Sub
Sub WatcherSetup()
fileName = TextBox1.Text
FileSystemWatcher1.IncludeSubdirectories = False
FileSystemWatcher1.Path = Path.GetDirectoryName(fileName)
FileSystemWatcher1.Filter = Path.GetFileName(fileName)
FileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite
FileSystemWatcher1.EnableRaisingEvents = True
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
WatcherSetup()
End Sub
Private Sub FileSystemWatcher1_Changed(sender As Object, e As FileSystemEventArgs) Handles FileSystemWatcher1.Changed
If TimerWatcherChanged.Enabled = False Then
TimerWatcherChanged.Enabled = True
Fsw_counter += 1
' ***** Your WATCH Code put here... *****
End If
End Sub
Private Sub TimerWatcherChanged_Tick(sender As Object, e As EventArgs) Handles TimerWatcherChanged.Tick
TimerWatcherChanged.Enabled = False
End Sub
Private Sub TimerTest_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerTest.Tick
TextBox2.Text = "Changed: " & Fsw_counter
If TimerWatcherChanged.Enabled = True Then
TextBox2.BackColor = Color.Red
Else
TextBox2.BackColor = Color.LawnGreen
End If
End Sub
End Class
I'm having trouble getting a progress bar to update. I'd be okay just with a moving marquee bar. Basically, I'm reading a database routine in a SqliteReader.vb class. I'm new to visual basic, and I'm sure I need to use the worker_DoWork routine, but I'm not sure how to expose my variables coming from Form1: graphData, graphComputations, m_debug to the worker_DoWork sub. How is this usually done?
Public Class SqliteReader
Public Sub ReadDataBase
End Sub
End Class
This is updating a graph (zedgraph element) on the main form, Form1.vb. I call the progressbar from the main form like this:
ProgressBar.Initialize(channelArray, computationArray, m_debug)
ProgressBar.vb below:
Partial Public Class ProgressBar
Dim DataAcquisition As New SqliteReader
Dim WithEvents worker As New BackgroundWorker
Public Sub Initialize(ByRef graphData As Channels(), ByRef graphComputations As Computations(), ByVal m_debug As Integer)
DataAcquisition = SqliteReader.GetInstance()
Me.Show()
Me.Update()
Dim Update_Thread As Thread(AddressOf Update_ThreadExecute)
Update_Thread.Priority = ThreadPriority.Normal
Update_Thread.Start()
DataAcquisition.ParseEntireDatabase(graphData, graphComputations, m_debug)
Me.Close()
End Sub
Private Sub ProgressBarStart(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataAcquisition = SqliteReader.GetInstance()
progress.Style = ProgressBarStyle.Marquee
worker.WorkerReportsProgress = True
worker.WorkerSupportsCancellation = True
worker.RunWorkerAsync()
End Sub
Private Sub worker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles worker.DoWork
Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker)
DataAcquisition = SqliteReader.GetInstance()
' I probably need
' DataAcquisition.ParseEntireDatabase(graphData, graphComputations, m_debug)
' here... but how do I expose graphdata, graphcomputations and m_debug to this sub?
End Sub
Private Sub worker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles worker.ProgressChanged
dataProgress = CInt(((DataAcquisition.currentRow + 1) / DataAcquisition.totalRows) * 100)
progress.Value = dataProgress
End Sub
RunWorkerAsync has a second version that takes a parameter. You can use that to pass any values (or references) your worker needs.
That said, you shouldn't be updating form elements from inside the worker. Your worker should fire the ProgressChanged event when you want the UI to update, and you handle it there. That one also has a version that can send a value back. (Or many values if you send back an array, list, or custom class.)
The last step in this is that you need to actually fire ProgressChanged. DataAcquisition.ParseEntireDatabase may not do that, in which case using it won't allow this method to work.
If graphData, graphComputations, m_debug are already members of ProgressBar and worker_DoWork is a member of ProgressBar, then you have nothing more to do. You should be able to access them directly.
For Rapunzo, above.. My Final Solution was this:
Partial Public Class ProgressBar
Dim _mDataAcquisition As New SqliteReader
Public Property DataProgress As Integer = 0
Dim WithEvents _mProgressWorker As New BackgroundWorker
Public Sub Initialize(ByRef graphData As List(Of Channels), ByRef auxData As List(Of Channels), _
ByRef graphComputations As List(Of Computations))
_mDataAcquisition = SqliteReader.GetInstance()
Show()
Update()
_mDataAcquisition.ParseEntireDatabase(graphData, auxData, graphComputations)
Close()
End Sub
Private Sub ProgressBarStart(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.Load
progress.Style = ProgressBarStyle.Blocks
_mProgressWorker.WorkerReportsProgress = True
_mProgressWorker.WorkerSupportsCancellation = True
_mProgressWorker.RunWorkerAsync()
progress.Visible = True
progress.Maximum = 100
progress.Value = 0
End Sub
Public Sub WorkerProgressChanged()
progress.Value = DataProgress
Invalidate()
End Sub
Private Sub WorkerRunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles _mProgressWorker.RunWorkerCompleted
progress.Visible = False
progress.Value = 0
Close()
End Sub
From there, just call ProgressBar.Initialize to start it
To update:
ProgressBar.DataProgress = CInt((currentIt / totalIt) * 100)
ProgressBar.WorkerProgressChanged()
and to end:
ProgressBar.DataProgress = 100
ProgressBar.WorkerProgressChanged()
Hope this helps.