Control.Visble = True ERROR

G

Guest

Hi

VB.NET 2005 (Express edition)

My form contains
Statusbar with progressbar and statuslabel
Command button
At design time the progressbar.Visible = False

At runtime I want to make the progressbar.Visble = True and run a method
which retrieves data from a "csv" file and populates it to a database. This
is triggered in either of two ways, 1 by the commandbutton or 2 by a
FileSystemWatcher method which is looking to see if a directory has received
a new file.

That's the background ..... The Problem? is with the visible property of the
progressbar control. Works fine when triggered by the commandbutton but
results in an error on that line when triggered by the FileSystemWatcher
change event.

My code is as follows:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
DirectoryWatcher()
watcher.EnableRaisingEvents = True
End Sub

Sub DirectoryWatcher()
watcher.Path = ("S:\Mypath")
'watcher.NotifyFilter = (NotifyFilters.LastAccess Or
NotifyFilters.LastWrite Or NotifyFilters.FileName Or
NotifyFilters.DirectoryName)
watcher.Filter = "*.csv"
AddHandler watcher.Changed, AddressOf OnChanged
End Sub

Private Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
PopulateData()
End Sub

Private Sub PopulateData()
ToolStripProgressBar1.Minimum = 0
ToolStripProgressBar1.Maximum = myRecordCount - 1
ToolStripProgressBar1.Value = 0
ToolStripStatusLabel1.Text = "Validating data"
ToolStripProgressBar1.Visible = True '......THIS IS THE OFFENDING
LINE'
Me.Refresh()

'and then a lot more code

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
PopulateData()
End Sub

and the error reads

"Controls created on one thread cannot be parented to a control on a
different thread."

Anyone know where I'm going wrong

Thanks

Michael Bond
 
C

Claes Bergefall

Sounds like the FileSystemWatcher components fires its events on a separate
thread. Try this:

Private Sub OnChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf PopulateData));
Else
PopulateData()
End If
End Sub

/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top