473,325 Members | 2,872 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,325 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 2176
"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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.