views:

158

answers:

1

Hi!

Please could you give me your thoughts on the following (especially if its advisable or not to do so)...

Basically, I can successfully import CSV data into a datatable and then bind that datatable to a datagridview. What I would like to do now is run through some validation checks for each row in the grid. Each row will have its data validated thru a Stored Procedure that will do a bunch of checks and return one of two values. I would then like to display this value in the last column (originally empty) of the grid, and then move on to the next row and repeat this action until all rows have been validated.

I hope this paints a clear picture of my intentions. In order to update the UI I would have to use the BackgroundWorker component, but am just concerned that this may not be the right way to go about it, and especially how to go about it.

Please advise me. Thank u!

+1  A: 

For a long operation, a background worker is the best way to perform a long task without making the GUI freeze.

You can use the worker's event ProgressChanged event to update the DataGrid. Note that you will have to update the DataGrid using the Invoke method, since GUI must be updated from the correct thread and Invoke transfers your action from the BG's thread to the GUI's thread.

Am
Cool! thanks Am! I attempted to tinker around with the BGW component before, but gave up (for some reason). Let me try again and get back to you. Also, would using a BGW component be the way to go for Database transactions? I have seen different opinions on this, but the app Im creating is distributed across a company's network, where there's bound to be some network latency.
Shalan
To my opinion, any operation that takes longer then 2 seconds has to be a background operation with a proper UI indication that something is happening.
Am
@Shalan I would recommend looking at SynchronizationContext's and the Post method as well as advise considering using BeginInvike instead of Invoke if SynchronizationContext is not going to be employed.
pst
@pst has a good point, __BeginInvoke__ is better then __Invoke__ in this situation. Never used the __SynchronizationContext__ so i can't say much about it.
Am
Ok, now u talking proper threading which is a bit beyond me at this point, but I remember seeing it explained well in a CodeProject article.
Shalan
So Im guessing then, that the BGW component is a mickey-mouse way of handling such scenarios? furthermore, I guess in any case involving long running tasks, proper threading techniques should be used. Am I right by this, or does the BGW component shipped with Visual Studio have some merit?
Shalan
BGW is nothing more then a control hiding a simple single thread mechanism. So it is the preferd control to be used for long operations. If you have a more complicated task, involving several thread (such as a server) then the BGW isn't good enough. But for a single long task it is just what you need, no magic behind it.
Am
It encapsulates a single thread, solves the common tasks of progress update and cancellation. Tasks you can accomplish on your own, but have a very standard solutions nowdays.
Am
thanx 4the info, but I dont plan on using more than just a single background thread to accomplish my tasks 'asynchronously'. I guess then that the BGW comtrol will suffice 4these operations. I just watched a video from the WindowsClient site involving the BGWcontrol similarly looping thru a list of items - seems to suit my purposes just fine :)
Shalan