473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cross thread (VB.NET 2005)

Hi,
I'm having a problem with my app. All its supposed to do is put the word
"Check" in a listbox every 2 seconds!
I'm getting a System.InvalidO perationExcepti on error, more specifically,
"Cross-thread operation not valid: Control 'ListBox1' accessed from a thread
other than the thread it was created on."

Please help! Here's my code...
Thanks, Paul

Imports System
Imports System.Timers

Public Class Form1

Dim mytimer As New System.Timers.T imer()

Private Sub btnStart_Click( ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnStart.Click
AddHandler mytimer.Elapsed , AddressOf OnTimedEvent
mytimer.Interva l = 2000
mytimer.Enabled = True
mytimer.Start()
End Sub

Private Sub btnStop_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnStop.Click
mytimer.Enabled = False
mytimer.Stop()
End Sub

Private Sub OnTimedEvent(By Val source As Object, ByVal e As
ElapsedEventArg s)
'My.Computer.Fi leSystem.WriteA llText("C:\test .log", "test", True)
ListBox1.Items. Add("Check..." & Date.Now())
End Sub
End Class
Mar 1 '07 #1
7 8155
>Dim mytimer As New System.Timers.T imer()

The easiest solution would be to use a System.Windows. Forms.Timer
instead of the System.Timers.T imer.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 1 '07 #2
Ok, I made that change, and changed the AddHandler mytimer.Elapsed ,
AddressOf OnTimedEvent line to read
AddHandler mytimer.Tick, AddressOf OnTimedEvent as VB said I should but now
I get this error on the same line...


hanged
"Mattias Sjögren" <ma************ ********@mvps.o rgwrote in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
Dim mytimer As New System.Timers.T imer()

The easiest solution would be to use a System.Windows. Forms.Timer
instead of the System.Timers.T imer.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Mar 1 '07 #3
Oops it would help to include the error!

Method 'Private Sub OnTimedEvent(so urce As Object, e As
System.Timers.E lapsedEventArgs )'
does not have the same signature as delegate 'Delegate Sub
EventHandler(se nder As Object,
e As System.EventArg s)'
Thanks,
Paul
"Paul" <ia*@home.co.uk wrote in message
news:t4******** *************@p ipex.net...
Ok, I made that change, and changed the AddHandler mytimer.Elapsed ,
AddressOf OnTimedEvent line to read
AddHandler mytimer.Tick, AddressOf OnTimedEvent as VB said I should but
now I get this error on the same line...


hanged
"Mattias Sjögren" <ma************ ********@mvps.o rgwrote in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
>Dim mytimer As New System.Timers.T imer()

The easiest solution would be to use a System.Windows. Forms.Timer
instead of the System.Timers.T imer.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Mar 1 '07 #4
"Paul" <ia*@home.co.uk wrote in
news:Qd******** *************** *******@pipex.n et:
I'm having a problem with my app. All its supposed to do is put the
word "Check" in a listbox every 2 seconds!
I'm getting a System.InvalidO perationExcepti on error, more
specifically, "Cross-thread operation not valid: Control 'ListBox1'
accessed from a thread other than the thread it was created on."
WIndows forms are single threaded - you need to use use Control.BeginIn voke
to marshal the call back to the proper thread.

Do a google search and you'll find plenty of examples.
Mar 1 '07 #5
Sorry, I've fixed it.

I wasn't using Timer1_Tick!

Using that instead of the OnTimedEvent sub now.

Private Sub Timer1_Tick(ByV al sender As Object, ByVal e As System.EventArg s)
Handles Timer1.Tick
ListBox1.Items. Add("Check..." & Date.Now())
Cheers!
Paul

"Paul" <ia*@home.co.uk wrote in message
news:t4******** *************@p ipex.net...
Ok, I made that change, and changed the AddHandler mytimer.Elapsed ,
AddressOf OnTimedEvent line to read
AddHandler mytimer.Tick, AddressOf OnTimedEvent as VB said I should but
now I get this error on the same line...


hanged
"Mattias Sjögren" <ma************ ********@mvps.o rgwrote in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
>Dim mytimer As New System.Timers.T imer()

The easiest solution would be to use a System.Windows. Forms.Timer
instead of the System.Timers.T imer.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Mar 1 '07 #6
"Spam Catcher" <sp**********@r ogers.comschrie b:
>I'm having a problem with my app. All its supposed to do is put the
word "Check" in a listbox every 2 seconds!
I'm getting a System.InvalidO perationExcepti on error, more
specifically , "Cross-thread operation not valid: Control 'ListBox1'
accessed from a thread other than the thread it was created on."

WIndows forms are single threaded - you need to use use
Control.BeginIn voke
to marshal the call back to the proper thread.

Do a google search and you'll find plenty of examples.
Articles and resources on the topic:

Multithreading in Windows Forms applications
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithread ing&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 2 '07 #7
Paul wrote:
Hi,
I'm having a problem with my app. All its supposed to do is put the word
"Check" in a listbox every 2 seconds!
I'm getting a System.InvalidO perationExcepti on error, more specifically,
"Cross-thread operation not valid: Control 'ListBox1' accessed from a thread
other than the thread it was created on."

You'll need to do a couple of steps:

1) Create an AddItemToList sub
2) define a delegate
3) create a delegate
4) check if the list needs to invoke before adding the item in the list
1)
Sub AddItemtoList(S as String)
lst.Items.Add(S )
end sub

2)
Private delegate sub _AddItem(S as String)

3)

Private AddItemDel as _AddItem
....
Sub New()
....
AddItemDel = new _AddItem(Addres sof AddItemToList)
....
end sub

4)
.....
Sub Somesub
if lst.invokerequi red then
lst.invoke(AddI temDel, StringToAdd)
else
AddItemToList(S tringToAdd)
end if
end sub

Using this you'll be able to call Somesub from withen your thread proc
with out any issues
Mar 2 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2378
by: [Yosi] | last post by:
What I should do to return back the permissin I had in VS2003 , that allowd me to access public methode of one Class from other Thread. I have Class A(FORM) which create new thread (new class B), the new class thread get as parameter reference to his father( CLASS who mad it (classA)), The new thread need to call methodes from his father like myfathe.methode(), This worked in VS 2003 but now in VS 2005 Beta throw an exception : An...
2
1280
by: TFS | last post by:
Hi, I know the topic of cross-thread operations from the UI has been addressed several times, but if anyone could provide some insight for my particular scenario, I would be thankful... Using Visual Basic (VS 2005 Beta 1) In a Public class X, I have a TCP connection setup to parse and dump data into a DataTable
4
3580
by: Bob | last post by:
Hi, Moving a project from .net 2003 -> 2005 Beta 2 Windows App. Main Window is start object. Main window spawns a thread. After doing some work this thread raises an interrupt. The event carries a reference to the class that raised it plus an instantiated, empty eventarg. RaiseEvent LineStateChange(Me, e) The 'RaiseEvent' errors in 2005 with Cross thread...
2
1340
by: genojoe | last post by:
In simplified form, I have an RTF control and a timer. I use the timer to do an autoresave for the RFT control every 5 minutes. Here is the abridged code. Private Sub tmrAutoRecover_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrAutoRecover.Tick Dim t As New Threading.Thread(New Threading.ThreadStart(AddressOf delDoAutoBackup)) t.IsBackground = True t.Start() End Sub
8
4857
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item I want to raise the ProgressChanged-event to update the DataGridView. It works fine when only one Progresschanged is fired, but at the second, third, fopurth etc it raises everytile a 'Cross-thread operation not valid"-exception on lmy...
11
8690
by: HairlipDog58 | last post by:
Hello, There are several 'cross-thread operation not valid' exception postings in the MSDN discussion groups, but none address my exact situation. I have authored a .NET component in Visual C# .NET 2003 that is capable of firing several types of events to notify user of errors, data changes, etc. The component uses a thread to perform background communications and if there is an error or data-change fires an event to notify the user....
3
5423
by: Pieter Coucke | last post by:
Hi, In my VB.NET 2005 application I'm generating and sending emails using the outlook-object model (2003). When a mail is Send (MailObject_Send), I raise an event in a global class, that is caught by all my forms that than refresh the lists with emails. But on the moment I do a MyDataGrid.DataSource = nothing, I get this "Cross-thread operation not valid"-exception:
4
5321
by: Paul Cheetham | last post by:
Hi, I have a couple of classes that I am using to read a swipe-card reader attached to the serial port (c# VS 2005 Pro). I have a SerialComm class which actaully reads the serial port, and a CardReader class which validates the number etc. Neither of these classes inherit from any other classes / controls. The SerialComm class is instantiated by the Cardreader class, so in the
3
30557
by: Le Minh | last post by:
I receive this message: "System.InvalidOperationException: Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on" .... this is a exception. Can fix it?
3
3747
by: kimiraikkonen | last post by:
Hi, I was looking for an example on CodeProject and saw an interesting thing, i downloaded the article source code and converted to my VB 2005 Express and compiled with no problem, however when i debug the program to and split a file(size doesn't matter) in debug mode with VS, that is, a file merge-split tool, nearly i always get a "cross- thread operation not valid" error for progress bar, whereas double clicking and launching program...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.