473,881 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Threadin g

Public Class UserControl1

Sub start()

Dim t As Thread = New Thread(AddressO f Me.test)

t.Start()

End Sub

Sub test()

Form1.ListView1 .Clear()

End Sub

End Class
Form code:
Public Class Form1

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
Handles Me.Load

ListView1.Items .Add("a")

ListView1.Items .Add("b")

UserControl11.s tart()

End Sub

End Class
Jun 26 '07 #1
5 2226
"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.Threadin g

Public Class UserControl1

Sub start()

Dim t As Thread = New Thread(AddressO f Me.test)

t.Start()

End Sub

Sub test()

If Form1.ListView1 .InvokeRequired Then

SyncLock Form1.ListView1 .GetType()

Form1.ListView1 .Invoke(CType(A ddressOf 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.Threadin g

Public Class UserControl1

Sub start()

Dim t As Thread = New Thread(AddressO f Me.test)

t.Start()

End Sub

Sub test()

If Form1.ListView1 .InvokeRequired Then

SyncLock Form1.ListView1 .GetType()

Form1.ListView1 .Invoke(CType(A ddressOf 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.Threadin g

Public Class UserControl1

Sub start()

Dim t As Thread = New Thread(AddressO f Me.test)

t.Start()

End Sub

Sub test()

If Form1.ListView1 .InvokeRequired Then

SyncLock Form1.ListView1 .GetType()

Form1.ListView1 .Invoke(CType(A ddressOf 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.Threadin g

Public Class UserControl1

Sub start()

Dim t As Thread = New Thread(AddressO f Me.test)

t.Start()

End Sub

Sub test()

If Form1.ListView1 .InvokeRequired Then

SyncLock Form1.ListView1 .GetType()

Form1.ListView1 .Invoke(CType(A ddressOf 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
3504
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 be appreciated. Simply leaving the threadMain() method if something unexpected occurs during thread execution leaves the thread in a ThreadState.Running state. This (apparently??) causes the Thread.Join() in service.OnStop() to hang...
2
1563
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, all 7.3.4, 2 on OS X and one on Linux. It builds fine. It installs fine. I go through the setup with no problems (calling createlang, doing the initial ers_setup, adding
7
2713
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 that. Problem is that when program is started many threads are created (see output section), when only two should be running at any time. Can you please help me to identify me where the problem is? Best regards
4
6211
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. Each task is processed in a separate thread. Each of the executer threads is an STA thread, and it goes ahead and executes the task. No problems are encountered when tasks are executed one at a time, but when multiple tasks are executed...
0
2064
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 a client. After receiving a Socket that has been connected to a RemoteEP, it begins 2 threads by calling out Begin(). My Send Thread pauses until the class Parent wants to send data. Receive Thread keeps checking for incoming data. My...
14
2968
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 component (created dynamically by, say, a button click, and then not referenced any more), the GC runs in a different thread, which prohibits the DLL to destroy its window, resulting in a GPF when the WndProc of that window is called - the code is gone...
10
2568
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 actually deserializing a SOAP document into an object and parsing out the fields, but that's fairly immaterial to this
3
1614
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 method: private void BUpisiStudente_Click(object sender, EventArgs e) {
11
2423
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 all the current documents using Application.Documents, the objects I got are not same ones stored previously. The event handler is called by some threads generated by C#. But, if I get all the current documents using my own threads, the ones...
5
2536
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 a multi-threaded application, naturally I want the call back functor to be created on a per thread basis. Suppose my thread is so defined template <typename servlet_type>
0
9928
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11100
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10816
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10402
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9555
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.