473,624 Members | 2,258 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 2205
"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
3493
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
1550
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
2700
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
6202
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
2051
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
2918
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
2537
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
1604
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
2406
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
2524
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
8170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8334
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
8474
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
7158
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
5561
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
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
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.