473,327 Members | 2,074 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,327 software developers and data experts.

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 "lvwFileList" and two buttons control to start and stops
a background process.
2) The backgroun process is done using the BackgroundWorkder control
and it calls a procedure named "ProcessFileList" 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 BackgroundWorker thread will result in a
"Cross-thread operation not valid"" error when the procedure attempts
to access "lvwFileList".

My next revision of the procedure uses methdodinvoke to get around the
"cross-thread" error:
Private Sub ProcessFileList
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf 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.Doevents() into the loop. But even with
Application.Doevents 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 ListviewDelegate".

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
FileSystemWatcher 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 3572
no****@nospam.com wrote in news:8m********************************@4ax.com:
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.com...
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 "lvwFileList" and two buttons control to start and stops
a background process.
2) The backgroun process is done using the BackgroundWorkder control
and it calls a procedure named "ProcessFileList" 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 BackgroundWorker thread will result in a
"Cross-thread operation not valid"" error when the procedure attempts
to access "lvwFileList".

My next revision of the procedure uses methdodinvoke to get around the
"cross-thread" error:
Private Sub ProcessFileList
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf 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.Doevents() into the loop. But even with
Application.Doevents 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 ListviewDelegate".

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
FileSystemWatcher 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**********@rogers.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.com wrote in news:fd********************************@4ax.com:
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
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,...
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
3
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...
0
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,...
4
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...
0
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,...
1
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...
6
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...
6
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...
6
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
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...

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.