473,785 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cross-thread operation not valid questions

Hi all.

I have encountered a "Cross-thread operation not valid" error in a
test program I created and although I have came up with a solution I
don't like the performance of the program.

I hope perhaps some experts here can help me out.

Here is what my program consists of:
1) A form containng several tabs, one of which contains a ListView
control named "lvwFileLis t" and two buttons control to start and stops
a background process.
2) The backgroun process is done using the BackgroundWorkd er control
and it calls a procedure named "ProcessFileLis t" which loops through
all items in the ListView control on the main form.

Here is my revision 1 of the procedure:
Private Sub ProcessFileList
Dim lvwItem as ListViewItem
For Each lvwItem in lvwFileList
'do some processing and update the ListView control
Next lvwItem
End Sub

Calling this procedure from a BackgroundWorke r thread will result in a
"Cross-thread operation not valid"" error when the procedure attempts
to access "lvwFileLis t".

My next revision of the procedure uses methdodinvoke to get around the
"cross-thread" error:
Private Sub ProcessFileList
If Me.InvokeRequir ed Then
Me.Invoke(New MethodInvoker(A ddressOf ProcessFileList ))
Else
Dim lvwItem as ListViewItem
For Each lvwItem in lvwFileList
'do some processing and update the ListView control
Next lvwItem
End If
End Sub

This procedure will invoke a delegate method to run in the main form's
thread and therefore can access the listview control directly. The
process will run but the main form's interface becomes unresponsive
unless I insert Application.Doe vents() into the loop. But even with
Application.Doe vents inserted, the form's user interface is still
sluggish. For example, jumping between different tabs on the form
takes about 2-3 seconds after each mouse click. FYI, the loop process
itsef does not use much CPU resources. According to Windows Task
Manager, the process contributed to only 3% of CPU utilization.
Does anyone know a better way to access a control from a
backgroundwoker thread?
Is it possible to create a delegate to a control?

I am interested in a solution that allows me to continue to use the
For ... Each statement to process and update the listview.

Something like "For Each lvwItem In ListviewDelegat e".

I already know how to access individual list view items in the by
using a delegate method. If I do that I will be forced to call the
delegate method in the loop for every listview item I want to access.
Also I will have to get the nubmer of listview items first and use the
regular For ... Next in the loop instead.

The reason I prefer the For ... Each loop is because I have a
FileSystemWatch er process running in the background that will add or
remove items from the listview control automatically. So if new items
are added to the listview control during the For ... Each loop
processing, they will also be included in the processing as long as
the For ... Each loop has not finished.

If there is no way to use For ... Each statement the way I would like,
I guess I will need to figure out how to make the main form more
responsive using my current approach.

Any comments or suggestions would be appreciated.

Thank you.
Jason

Feb 9 '07 #1
5 3601
no****@nospam.c om wrote in news:8m******** *************** *********@4ax.c om:
Does anyone know a better way to access a control from a
backgroundwoker thread?
Is it possible to create a delegate to a control?
You only need to "invoke" when updating something on the form.

Thus, if the majority of your background thread is looping, calculating,
fetching data... then you can only call "invoke" when you update an element
(i.e. set an item text property etc.).
Feb 9 '07 #2

Well this can be solved verry easy , just load the items in a array , data
table , hashtable or whatever
pefform your operations with the items in this .

as last action you set them to the listview control for your user to see
with a delagate

regards

Michel

<no****@nospam. comschreef in bericht
news:8m******** *************** *********@4ax.c om...
Hi all.

I have encountered a "Cross-thread operation not valid" error in a
test program I created and although I have came up with a solution I
don't like the performance of the program.

I hope perhaps some experts here can help me out.

Here is what my program consists of:
1) A form containng several tabs, one of which contains a ListView
control named "lvwFileLis t" and two buttons control to start and stops
a background process.
2) The backgroun process is done using the BackgroundWorkd er control
and it calls a procedure named "ProcessFileLis t" which loops through
all items in the ListView control on the main form.

Here is my revision 1 of the procedure:
Private Sub ProcessFileList
Dim lvwItem as ListViewItem
For Each lvwItem in lvwFileList
'do some processing and update the ListView control
Next lvwItem
End Sub

Calling this procedure from a BackgroundWorke r thread will result in a
"Cross-thread operation not valid"" error when the procedure attempts
to access "lvwFileLis t".

My next revision of the procedure uses methdodinvoke to get around the
"cross-thread" error:
Private Sub ProcessFileList
If Me.InvokeRequir ed Then
Me.Invoke(New MethodInvoker(A ddressOf ProcessFileList ))
Else
Dim lvwItem as ListViewItem
For Each lvwItem in lvwFileList
'do some processing and update the ListView control
Next lvwItem
End If
End Sub

This procedure will invoke a delegate method to run in the main form's
thread and therefore can access the listview control directly. The
process will run but the main form's interface becomes unresponsive
unless I insert Application.Doe vents() into the loop. But even with
Application.Doe vents inserted, the form's user interface is still
sluggish. For example, jumping between different tabs on the form
takes about 2-3 seconds after each mouse click. FYI, the loop process
itsef does not use much CPU resources. According to Windows Task
Manager, the process contributed to only 3% of CPU utilization.
Does anyone know a better way to access a control from a
backgroundwoker thread?
Is it possible to create a delegate to a control?

I am interested in a solution that allows me to continue to use the
For ... Each statement to process and update the listview.

Something like "For Each lvwItem In ListviewDelegat e".

I already know how to access individual list view items in the by
using a delegate method. If I do that I will be forced to call the
delegate method in the loop for every listview item I want to access.
Also I will have to get the nubmer of listview items first and use the
regular For ... Next in the loop instead.

The reason I prefer the For ... Each loop is because I have a
FileSystemWatch er process running in the background that will add or
remove items from the listview control automatically. So if new items
are added to the listview control during the For ... Each loop
processing, they will also be included in the processing as long as
the For ... Each loop has not finished.

If there is no way to use For ... Each statement the way I would like,
I guess I will need to figure out how to make the main form more
responsive using my current approach.

Any comments or suggestions would be appreciated.

Thank you.
Jason

Feb 10 '07 #3
On Fri, 09 Feb 2007 22:03:42 GMT, Spam Catcher
<sp**********@r ogers.comwrote:
>You only need to "invoke" when updating something on the form.

Thus, if the majority of your background thread is looping, calculating,
fetching data... then you can only call "invoke" when you update an element
(i.e. set an item text property etc.).
Okay.

I ended up creating several methods which I invoke in my loop using
the main form's thread whenever I need to read, update, set item
color, and so on in the listview control. It looks messy but it
works.

Thanks for the suggestion.
Jason

Feb 13 '07 #4
On Sat, 10 Feb 2007 09:57:47 +0100, "Michel Posseth [MCP]"
<ms****@posseth .comwrote:
>
Well this can be solved verry easy , just load the items in a array , data
table , hashtable or whatever
pefform your operations with the items in this .

as last action you set them to the listview control for your user to see
with a delagate
You approach would work for me except I have to change font colors on
certain items in the listview. Instead of creating another array to
keep track of item colors, I just invoke different methods in my loop
to set color directly on the listview.

Thanks for the suggestion though.
Jason

Feb 13 '07 #5
no****@nospam.c om wrote in news:fd******** *************** *********@4ax.c om:
I ended up creating several methods which I invoke in my loop using
the main form's thread whenever I need to read, update, set item
color, and so on in the listview control. It looks messy but it
works.
Ya - that's how it like :-(
Feb 13 '07 #6

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

Similar topics

0
2055
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics, manifolds, science, physics, chemistry, law, legal, government, home, office, business, domain lookup, medical, travel, food, university students, searching, searchers, surfing, advanced search, search tools Chemistry, mathematics, physical sciences,...
12
3880
by: * ProteanThread * | last post by:
but depends upon the clique: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=954drf%24oca%241%40agate.berkeley.edu&rnum=2&prev=/groups%3Fq%3D%2522cross%2Bposting%2Bversus%2Bmulti%2Bposting%2522%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den ...
3
3114
by: rollasoc | last post by:
Hi, Doing a bit of system testing on a Windows 98 laptop. (.Net 1.1 app). Did a bit of testing. Loaded a previously saved file. A gray box appeared with the text and buttons all white rectangles with a big red cross in it. Pressed a button (the one I thought might be ok). My file appeared to load. Then when I clicked on any button on my form, the button was replaced with a white rectangle with a big red cross in it.
0
1895
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics, manifolds, science, physics, chemistry, law, legal, government, home, office, business, domain lookup, medical, travel, food, university students, searching, searchers, surfing, advanced search, search tools Chemistry, mathematics, physical sciences,...
4
5584
by: David Peach | last post by:
Hello, hope somebody here can help me... I have a query that lists defects recorded in a user defined date range. That query is then used as the source for a Cross Tab query that cross-tabs count of defect type by calendar month. Defect types are stored in one table, defect transactions in another along with date etc. When I cross-tab the results, defect types that have no defects recorded against them appear as a blank (null) value. That...
0
2059
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics, manifolds, science, physics, chemistry, law, legal, government, home, office, business, domain lookup, medical, travel, food, university students, searching, searchers, surfing, advanced search, search tools Chemistry, mathematics, physical sciences,...
1
2766
by: Rob Woodworth | last post by:
Hi, I'm having serious problems getting my report to work. I need to generate a timesheet report which will contain info for one employee between certain dates (one week's worth of dates). I have a table containing records for each job done, the records contain date, employee name, job done (a code representing the type of job), cost code (another code), regular hours, and overtime hours. The tricky part is that more than one job can...
6
8635
by: Simon | last post by:
Hi All, An experiment i'm doing requires requires a synchronous cross-domain request, without using a proxy. I wondered if anyone had any ideas to help me achieve this. Below is what I have tried, including my conclusions/assumptions (which i'll happily be corrected on if it solves my problem!): The requirement not to use a proxy means I can't use the synchronous
6
5483
by: Bart Van der Donck | last post by:
Hello, I'm presenting my new library 'AJAX Cross Domain' - a javascript extension that allows to perform cross-domain AJAX requests. http://www.ajax-cross-domain.com/ Any comments or suggestions are welcome. --
6
3992
by: ampo | last post by:
Hello. Can anyone help with cross-domain problem? I have HTML page from server1 that send xmlHTTPRequest to server2. How can I do it? Thanks.
0
9646
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
9484
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,...
0
10157
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...
0
9957
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
8983
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
6742
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
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.