Wrox Programmer Forums
|
Visual Basic 2008 Essentials If you are new to Visual Basic programming with version 2008, this is the place to start your questions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2008 Essentials section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 16th, 2009, 02:33 PM
Authorized User
 
Join Date: Oct 2006
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ricespn
Default Cross-Thread Error

Guys,

I have an application that used to run just fine on .NET 2.0 but now that I'm trying to migrate it to .NET 3.5 I'm getting this error: Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on.


Here is the code:

Code:
Imports System.IO
Imports System.Diagnostics
PublicClass Form1
Public watchFolder As FileSystemWatcher
 
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
watchFolder = New System.IO.FileSystemWatcher()
'Path and file type
watchFolder.Path = "C:\XML\"
watchFolder.Filter = "*.XML"
'Filters
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName Or NotifyFilters.Attributes Or NotifyFilters.DirectoryName
' Event Handlers
AddHandler watchFolder.Changed, AddressOf fileChange
AddHandler watchFolder.Created, AddressOf fileChange
AddHandler watchFolder.Deleted, AddressOf fileChange
AddHandler watchFolder.Renamed, AddressOf fileRename
watchfolder.EnableRaisingEvents = True
Button1.Enabled = False
Button2.Enabled = True
EndSub
PrivateSub fileChange(ByVal source AsObject, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
TextBox1.Text &= "File " & e.FullPath & " has been modified" & vbCrLf
EndIf
If e.ChangeType = IO.WatcherChangeTypes.Created Then
TextBox1.Text &= "File " & e.FullPath & " has been created" & vbCrLf
EndIf
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
TextBox1.Text &= "File " & e.FullPath & " has been deleted" & vbCrLf
EndIf
EndSub
PublicSub fileRename(ByVal source AsObject, ByVal e As System.IO.RenamedEventArgs)
TextBox1.Text &= "File" & e.OldName & " has been renamed to " & e.Name & vbCrLf
EndSub
PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Stop watching the folder
watchfolder.EnableRaisingEvents = False
Button1.Enabled = True
Button2.Enabled = False
EndSub
EndClass

__________________
=======================
Strange and crazy, but everything is possible





Similar Threads
Thread Thread Starter Forum Replies Last Post
cross thread exception YoungLuke C# 1 March 23rd, 2008 06:46 PM
cross thread exception with windows live API YoungLuke C# 2008 aka C# 3.0 1 March 23rd, 2008 01:43 PM
cross thread Error angelboy C# 2005 1 July 28th, 2007 04:35 AM
Cross-thread operation not valid Richw71 C# 0 December 13th, 2006 09:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.