Problem with thread

N

nondos

Hello i have problem with thread,

I have Form with listview and User control.
I need the User control to run thread that will clear the listview in the
main form.
the code work but it's not clearing the listview, i debug the code and i see
the line of clearing the listview execute and i don't get any error message.
if i call the test sub directly from the form not as thread it's work fine.
any idea what's wrong

Thanks

control code:

Imports System.Threading

Public Class UserControl1

Sub start()

Dim t As Thread = New Thread(AddressOf Me.test)

t.Start()

End Sub

Sub test()

Form1.ListView1.Clear()

End Sub

End Class


Form code:
Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

ListView1.Items.Add("a")

ListView1.Items.Add("b")

UserControl11.start()

End Sub

End Class
 
A

Armin Zingler

nondos said:
Hello i have problem with thread,

I have Form with listview and User control.
I need the User control to run thread that will clear the listview
in the main form.
the code work but it's not clearing the listview, i debug the code
and i see the line of clearing the listview execute and i don't get
any error message. if i call the test sub directly from the form not
as thread it's work fine. any idea what's wrong

Only the thread that creates a control is allowed to access it. Have a look
at the control's Invoke/BeginInvoke methods (which don't make much sense in
your example, but in general).


Armin
 
N

nondos

thanks for the replay

i tried to use invoke still not working here's the User Control code after i
changed it:

Imports System.Threading

Public Class UserControl1

Sub start()

Dim t As Thread = New Thread(AddressOf Me.test)

t.Start()

End Sub

Sub test()

If Form1.ListView1.InvokeRequired Then

SyncLock Form1.ListView1.GetType()

Form1.ListView1.Invoke(CType(AddressOf Me.clear, MethodInvoker))

End SyncLock

End If

End Sub

Sub clear()

Form1.ListView1.Clear()

End Sub

End Class



The code i showed was just example for what i'm trying to do it's not the
real code as u can
Imagine
I have listview and i need to update it as there are threads in the
background running that effect the listview.

what will be the best way to do it?

P.S. the threads are part of USER control that i add to the main form that
have the list view

thanks
 
P

pamela fluente

thanks for the replay

i tried to use invoke still not working here's the User Control code after i
changed it:

Imports System.Threading

Public Class UserControl1

Sub start()

Dim t As Thread = New Thread(AddressOf Me.test)

t.Start()

End Sub

Sub test()

If Form1.ListView1.InvokeRequired Then

SyncLock Form1.ListView1.GetType()

Form1.ListView1.Invoke(CType(AddressOf Me.clear, MethodInvoker))

End SyncLock

End If

End Sub

Sub clear()

Form1.ListView1.Clear()

End Sub

End Class

The code i showed was just example for what i'm trying to do it's not the
real code as u can
Imagine
I have listview and i need to update it as there are threads in the
background running that effect the listview.

what will be the best way to do it?

P.S. the threads are part of USER control that i add to the main form that
have the list view

thanks

Place the test code in a button click handler to check the possibility
that the thread is invoked before the fill of the listview.

-P
 
A

Armin Zingler

nondos said:
thanks for the replay

i tried to use invoke still not working here's the User Control code
after i changed it:

Imports System.Threading

Public Class UserControl1

Sub start()

Dim t As Thread = New Thread(AddressOf Me.test)

t.Start()

End Sub

Sub test()

If Form1.ListView1.InvokeRequired Then

SyncLock Form1.ListView1.GetType()

Form1.ListView1.Invoke(CType(AddressOf Me.clear, MethodInvoker))

End SyncLock

End If

End Sub

Sub clear()

Form1.ListView1.Clear()

End Sub

End Class



The code i showed was just example for what i'm trying to do it's
not the real code as u can
Imagine
I have listview and i need to update it as there are threads in the
background running that effect the listview.

what will be the best way to do it?

P.S. the threads are part of USER control that i add to the main
form that have the list view

thanks


Your code works here. Maybe "Form1" points to a different Form1 object?


Armin
 
B

Brian Gideon

thanks for the replay

i tried to use invoke still not working here's the User Control code after i
changed it:

Imports System.Threading

Public Class UserControl1

Sub start()

Dim t As Thread = New Thread(AddressOf Me.test)

t.Start()

End Sub

Sub test()

If Form1.ListView1.InvokeRequired Then

SyncLock Form1.ListView1.GetType()

Form1.ListView1.Invoke(CType(AddressOf Me.clear, MethodInvoker))

End SyncLock

End If

End Sub

Sub clear()

Form1.ListView1.Clear()

End Sub

End Class

The code i showed was just example for what i'm trying to do it's not the
real code as u can
Imagine
I have listview and i need to update it as there are threads in the
background running that effect the listview.

what will be the best way to do it?

P.S. the threads are part of USER control that i add to the main form that
have the list view

thanks

I'm not understanding the point of the thread. Regardless, it's not
necessary to wrap Invoke with a SyncLock. Invoke is already thread-
safe. Also, as written, the SyncLock would have been unnecessarily
restrictive since the lock is acquired on the object that represents
the type of the ListView instead of the actual ListView instance.

Brian
 

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