473,320 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.InvalidOperationException 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.Timer()

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

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

Private Sub OnTimedEvent(ByVal source As Object, ByVal e As
ElapsedEventArgs)
'My.Computer.FileSystem.WriteAllText("C:\test.log" , "test", True)
ListBox1.Items.Add("Check..." & Date.Now())
End Sub
End Class
Mar 1 '07 #1
7 8139
>Dim mytimer As New System.Timers.Timer()

The easiest solution would be to use a System.Windows.Forms.Timer
instead of the System.Timers.Timer.
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.orgwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Dim mytimer As New System.Timers.Timer()

The easiest solution would be to use a System.Windows.Forms.Timer
instead of the System.Timers.Timer.
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(source As Object, e As
System.Timers.ElapsedEventArgs)'
does not have the same signature as delegate 'Delegate Sub
EventHandler(sender As Object,
e As System.EventArgs)'
Thanks,
Paul
"Paul" <ia*@home.co.ukwrote in message
news:t4*********************@pipex.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.orgwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>Dim mytimer As New System.Timers.Timer()

The easiest solution would be to use a System.Windows.Forms.Timer
instead of the System.Timers.Timer.
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.ukwrote in
news:Qd******************************@pipex.net:
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.InvalidOperationException 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.BeginInvoke
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(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Timer1.Tick
ListBox1.Items.Add("Check..." & Date.Now())
Cheers!
Paul

"Paul" <ia*@home.co.ukwrote in message
news:t4*********************@pipex.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.orgwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>Dim mytimer As New System.Timers.Timer()

The easiest solution would be to use a System.Windows.Forms.Timer
instead of the System.Timers.Timer.
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**********@rogers.comschrieb:
>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.InvalidOperationException 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.BeginInvoke
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=multithreading&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.InvalidOperationException 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(Addressof AddItemToList)
....
end sub

4)
.....
Sub Somesub
if lst.invokerequired then
lst.invoke(AddItemDel, StringToAdd)
else
AddItemToList(StringToAdd)
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
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),...
2
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...
4
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...
2
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...
8
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...
11
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#...
3
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...
4
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...
3
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....
3
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.