Cross thread (VB.NET 2005)

P

Paul

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
 
M

Mattias Sjögren

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
 
P

Paul

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
 
P

Paul

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 said:
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
 
G

Guest

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.
 
P

Paul

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 said:
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
 
L

Logie Urquhart

Paul said:
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
 

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