473,387 Members | 1,890 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,387 software developers and data experts.

Problem with thread

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
Jun 26 '07 #1
5 2183
"nondos" <no****@nondos.orgschrieb
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

Jun 26 '07 #2
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

Jun 26 '07 #3
On 26 Giu, 17:32, "nondos" <non...@nondos.orgwrote:
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
Jun 26 '07 #4
"nondos" <no****@nondos.orgschrieb
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
Jun 26 '07 #5
On Jun 26, 10:32 am, "nondos" <non...@nondos.orgwrote:
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

Jun 26 '07 #6

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

Similar topics

0
by: David | last post by:
I've written a small windows service, and I'm having a problem that I'm spending a lot more time on than I'd like. If anyone has experienced this problem and has any hints or solutions; they would...
2
by: Andrew Rawnsley | last post by:
Downloaded the eRServer 1.2 source (thanks, guys) and installed, and am having a problem. Anyone else dug into this yet? Situation: PG 7.3.4 on linux (master) Its been tried with 3 slaves,...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
4
by: Madhu Gopinathan | last post by:
Hi All, I am faced with a horrible hang problem. I have a COM exe server that executes some tasks. The task execution manager is a thread that manages the pool of threads, which is 4 per processor....
0
by: Shoveler | last post by:
I've got an odd problem here that I've been beating my head on for days. I've written a class that uses a pre-established connection for communication, meaning I can use this class for a server or...
14
by: Christian Kaiser | last post by:
We have a component that has no window. Well, no window in managed code - it uses a DLL which itself uses a window, and this is our problem! When the garbage collector runs and removes our...
10
by: Bryce Calhoun | last post by:
Hello, First of all, this is a .NET 1.1 component I'm creating. SUMMARY ----------------------- This component that I'm creating is, for all intents and purposes, a document parser (I'm...
3
by: bilosta | last post by:
Hello to everybody I'm new in win apllications and C#. I have a problem with Threading. Here is my problem: I have a form with button named: BupisiStudente, when I click on it a call next...
11
by: leiz | last post by:
Hi, I am using COM to automate Word. In the main thread of my program, I store all the Document objects from Application.Documents. In one event handler (the event generated by Word), if i get...
5
by: Fei Liu | last post by:
Hello, I have a situation where I need to design a library for multi-thread application, each thread does some work in a client supplied std::ptr_fun(free_function) or a functor. Now since it's...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.