473,386 Members | 1,775 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,386 software developers and data experts.

DataGridView.Refresh() hangs on cross-thread call

Hi all,

I'm having a weird time with a call to the Refresh() method of a
DataGridView. I have a VB.NET 2005 Windows application with a main form and
a "worker" thread. The main form delegates a Sub to the worker so that the
latter can refresh the DataGrid after updating the DataTable driving it.

When I run the app under the debugger, everything's fine. But when I run it
for real, it hangs, apparently in the Refresh() method of the DataGridView -
that is, as soon as I comment out dgRefresh, the hanging goes away.

I'm dealing with the cross-thread issues appropriately, as far as I'm aware:

Public Delegate Sub dgExtRefreshCallback()
Public Sub dgExtRefresh()
dgExtensions.Refresh()
End Sub
Private Sub refreshDGExtensions()
If dgExtensions.InvokeRequired Then
Dim d As New dgExtRefreshCallback(AddressOf dgExtRefresh)
Me.Invoke(d, New Object() {})
Else
dgExtRefresh()
End If
End Sub

.... so it's "refreshDGExtensions()" that is delegated to the worker
class/thread.

I can't help thinking that there's a timing issue somewhere if it's working
in the debugger but not when run from the desktop. I figured I'd throw some
locking in to ensure that (for instance) the DataTable behind the DataGrid
didn't get updated while the DataGrid was refreshing, but that didn't seem
to help. So any ideas would be most welcome!

Thanks a lot,

David C
Oct 25 '06 #1
3 7447
Hi David,

Based on my understanding, you have WinForms application using multithread.
You refresh the data grid view in a thread other than the application's UI
thread. If the program is started with debugger, every thing works fine.
However, if the program is run from the desktop, it hangs in the Refresh
method of the data grid view. As soon as you comment out the statement of
calling the DataGridView.Refresh method, the hanging goes away. If I'm off
base, please feel free to correct me.

Firstly, I'd like to say that your dealing with the cross-thread issue is
appropriate. To ensure that access to your controls is done in a
thread-safe way, we should use the Invoke method of the control.

It seems that race conditions or deadlocks occur in your program from the
phenomenon of the program's hanging. For example, you have locked the data
grid view or data table otherwhere and you require the lock on the data
grid view or data table before you refresh the data grid view.

I think the reason why the problem doesn't occur when the program is
started with debugger is that the conditions for race conditions or
deadlocks aren't met when the program is run with debugger.

I suggest that you check the code in your program to find all places where
the data grid view or data table is locked and then correct them.

Please try my suggestion. If the problem isn't solved, you may make a
sample project that could reproduce the problem and send it to me. To get
my actual email address, remove 'online' from my displayed email address.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 26 '06 #2
"Linda Liu [MSFT]" <v-****@online.microsoft.comwrote in message
news:4c****************@TK2MSFTNGXA01.phx.gbl...

Hi Linda,
Firstly, I'd like to say that your dealing with the cross-thread issue is
appropriate. To ensure that access to your controls is done in a
thread-safe way, we should use the Invoke method of the control.
It seems that race conditions or deadlocks occur in your program from the
phenomenon of the program's hanging. For example, you have locked the data
grid view or data table otherwhere and you require the lock on the data
grid view or data table before you refresh the data grid view.
I think the reason why the problem doesn't occur when the program is
started with debugger is that the conditions for race conditions or
deadlocks aren't met when the program is run with debugger.
Hi Linda,

Yes, that's my suspicion too. Something somewhere is getting deadlocked, but
under the debugger the timing is such that I'm getting away with it - I
figured that I'd double-check that it wasn't a known .NET bug and that I
wasn't doing anything stupid!

I'll keep digging!

All the best,

D.
Oct 26 '06 #3
Hi,

"David Cartwright" <ds**********@community.nospamwrote in message
news:eb****************@TK2MSFTNGP05.phx.gbl...
Hi all,

I'm having a weird time with a call to the Refresh() method of a
DataGridView. I have a VB.NET 2005 Windows application with a main form
and a "worker" thread. The main form delegates a Sub to the worker so that
the latter can refresh the DataGrid after updating the DataTable driving
it.

When I run the app under the debugger, everything's fine. But when I run
it for real, it hangs, apparently in the Refresh() method of the
DataGridView - that is, as soon as I comment out dgRefresh, the hanging
goes away.

I'm dealing with the cross-thread issues appropriately, as far as I'm
aware:

Public Delegate Sub dgExtRefreshCallback()
Public Sub dgExtRefresh()
dgExtensions.Refresh()
End Sub
Private Sub refreshDGExtensions()
If dgExtensions.InvokeRequired Then
Dim d As New dgExtRefreshCallback(AddressOf dgExtRefresh)
Me.Invoke(d, New Object() {})
Else
dgExtRefresh()
End If
End Sub

... so it's "refreshDGExtensions()" that is delegated to the worker
class/thread.

I can't help thinking that there's a timing issue somewhere if it's
working in the debugger but not when run from the desktop. I figured I'd
throw some locking in to ensure that (for instance) the DataTable behind
the DataGrid didn't get updated while the DataGrid was refreshing, but
that didn't seem to help. So any ideas would be most welcome!
Why do you even need to call Refresh ?

If the DataTable is bound to the DataGridView, then you need to use
Control.Invoke to update the DataTable, then the grid will refresh itself.
If you are inserting rows continous then you'll need to set the worker
thread priority to BelowNormal to give the UI thread the chance to update
properly.

In addition you can also batch the inserts (in a temp table) 10 a 50 rows
then merge them with the DataTable bound to the DGV using Control.Invoke.

HTH,
Greetings
Thanks a lot,

David C


Oct 26 '06 #4

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

Similar topics

0
by: thomasp | last post by:
This is a two part question, 1) The code below should display a form with a datagridview and a few command buttons. This form should allow the user to make change to the records displayed in...
1
by: RSH | last post by:
Hi, I have a situation where I have a DDL that lists all of the tables in a particular database, and a Datagridview which appears in the same from below the DDL.. When the user selects a new...
6
by: George | last post by:
Hi, I have been encountering a refresh problem with DataGridView, which is bound to a DataTable. When I make updates (Add, Delete, update) to the DataGridView, everything flow nicely to...
3
by: Meelis | last post by:
Hi Has ŽDataGridView row limits? I can't add about 1150 rows from XML file to DataGridView, it just "hangs" Regards; Mex
4
by: =?Utf-8?B?UmljaA==?= | last post by:
On a form - I have a datagridview which is docked to the entire form. The datagridview allows users to Delete and/or Add Rows. On the Form_Load event I Fill the datagridview source table with a...
1
by: Karl | last post by:
Hi all... This is a good one. You'll like this... I am working on a course management tool that allows certain Courses to be cross referenced with Job Roles and, when they are, whether the...
2
by: Andrus | last post by:
VSE 2005 .NET 2 WinForms I have DataGridView in virtual mode containing huge number of rows. When user double clicks in column separator line in grid header, application hangs: grid tries to...
2
by: YouPoP | last post by:
Hi, I have a Window Form to which i added a datagridview. The binding source is working well, but does not update if the database is modified outside the application (i mean not showing the...
5
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I want to be able to increase or decrease row heights of a populated DataGridView from the keyboard. I set up a test program with menu items to increase and decrease, assigned shortkey keys...
0
by: enrico via DotNetMonster.com | last post by:
when i click the delete record i want my datagridview to automatically clear the deleted record or refresh my DGV because even though it is already deleted in the database the datagridview doesn't...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.