how to write to UI after each row has been processed?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a windows form with a button and a richtext box
On click of a button ,I am looping through a datatable which has 3600 rows
and i am checking some code for each column in the row.There are 300
columns.
So total iteraations are 3600*300=960000
eg

for(i=0;i<rows.count;i++){
//get a datarow
for(j=0;j<rows.columns.count;j++)
{
if(datarow[j]=xyz) {
//some code
//write to textbox on windows form
}
}
//write row details to textbox
}
I am calling this code in the btn_click event.

My problem is that after I click the button my UI seems to hang and nothing
is shown on the textbox til all processing is over.When I run the code for
only 2 rows I get the output on the textbox fast but again after it has
finished processing the 2 rows.

My qn is how to make the code write to the UI after it has finished
prcessing one row?I know I need to use multi threading but I am a newbie and
pretty much confused about it.

I am using Visual studio 2003 and C# 1.x

Pls help.
Thanks in advance.
 
cooltech77 said:
My qn is how to make the code write to the UI after it has finished
prcessing one row?I know I need to use multi threading but I am a newbie and
pretty much confused about it.

You can avoid multithreading if you want. Just call
Application.DoEvents() after each row. It's not as nice as
multithreading but it is easier!

NB. Application.DoEvents() will make the UI respond to events --
repainting, moving, button-clicks. What will happen if you're inside
your button-click handler, then you call DoEvents(), then the user
clicks the button a second time? Disaster! So you'll need to figure
out an architecture/design where you disallow this kind of thing from
happening. (multithreading would still require the same kind of
architecture anyway so you have to just bite the bullet and do it.)
 
Hello cooltech77,

Jon and Peter have articles about this topic
see there http://www.eggheadcafe.com/articles/20050926.asp and http://www.yoda.arachsys.com/csharp/threads/winforms.shtml

c> Hi,
c>
c> I have a windows form with a button and a richtext box
c> On click of a button ,I am looping through a datatable which has 3600
c> rows
c> and i am checking some code for each column in the row.There are
c> 300
c> columns.
c> So total iteraations are 3600*300=960000
c> eg
c> for(i=0;i<rows.count;i++){
c> //get a datarow
c> for(j=0;j<rows.columns.count;j++)
c> {
c> if(datarow[j]=xyz) {
c> //some code
c> //write to textbox on windows form
c> }
c> }
c> //write row details to textbox
c> }
c> I am calling this code in the btn_click event.
c>
c> My problem is that after I click the button my UI seems to hang and
c> nothing is shown on the textbox til all processing is over.When I run
c> the code for only 2 rows I get the output on the textbox fast but
c> again after it has finished processing the 2 rows.
c>
c> My qn is how to make the code write to the UI after it has finished
c> prcessing one row?I know I need to use multi threading but I am a
c> newbie and pretty much confused about it.
c>
c> I am using Visual studio 2003 and C# 1.x
c>
c> Pls help.
c> Thanks in advance.
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Back
Top