Home All Groups Group Topic Archive Search About

Control.Visble = True ERROR

Author
16 Aug 2006 1:48 PM
mabond
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

Author
16 Aug 2006 2:29 PM
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

Show quoteHide quote
"mabond" <mab***@discussions.microsoft.com> wrote in message
news:1792BF0A-0904-44E4-8D6C-6332BDCBAF9F@microsoft.com...
> 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
Author
16 Aug 2006 2:39 PM
mabond
Claes

Much obliged. That solved the problem.

Thanks

Michael Bond

Show quoteHide quote
"Claes Bergefall" wrote:

> 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
>
> "mabond" <mab***@discussions.microsoft.com> wrote in message
> news:1792BF0A-0904-44E4-8D6C-6332BDCBAF9F@microsoft.com...
> > 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
>
>
>