RichTextBox and other controls wont update

  • Thread starter Thread starter KK
  • Start date Start date
K

KK

this is a standard behaviour in the sense that your long running operation
is on the UI thread and its blocking the UI thread. So untill all is over,
it wont update anythig. As mentioned in other reply, think of
Application.DoEvents or better if you can implement these things in separate
threads.
 
Hi everyone,

I have a loop that runs thousands of time an does IO operations. in
each loop I update RichTextBox.Text but it does not take effect until
the end of whole loop. It occurs about other controls too. How can I
enforce these controls to be updated correctly?

I'm using:
VS 2005,
C#,
framework 2.0
Nhibernate


Thanks in Advance:
Afshar Mohebbi
 
Hi everyone,

I have a loop that runs thousands of time an does IO operations. in
each loop I update RichTextBox.Text but it does not take effect until
the end of whole loop. It occurs about other controls too. How can I
enforce these controls to be updated correctly?

I'm using:
VS 2005,
C#,
framework 2.0
Nhibernate


Thanks in Advance:
Afshar Mohebbi

Try Application.DoEvents() in the loop - it may slow it down though.
 
Hi Afshar,

Can you provide peace of code?

Anyway it's a bad idea to write into controls every iteration step. Better
soultion is to create some state-object to store iterations data and reflect
changes to presentation level afterward. You will control execution in one
place and improve perfomance, because you should know about rendering if
visible modification occurs.

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



AM> Hi everyone,
AM>
AM> I have a loop that runs thousands of time an does IO operations. in
AM> each loop I update RichTextBox.Text but it does not take effect
AM> until the end of whole loop. It occurs about other controls too. How
AM> can I enforce these controls to be updated correctly?
AM>
AM> I'm using:
AM> VS 2005,
AM> C#,
AM> framework 2.0
AM> Nhibernate
AM> Thanks in Advance:
AM> Afshar Mohebb
 
Dear Friends,


I tried Application.DoEvents() and it worked fine. But it is very
slow. Is there a better way? What is "some state-object"? follwoing is
my complete code:

private Hashtable ImportOrganization(string sheetName,
ExcelUtility excelUtility)
{
Hashtable result = new Hashtable();

const int nameCol = 1;
const int orgIdCol = 2;
const int telCol = 3;
const int faxCol = 4;
const int addressCol = 5;

excelUtility.LoadWorksheet(sheetName);
int rowIndex = startingRow;
string rowIdentifier = excelUtility.ReadCell(rowIndex,
startingCol);
while (!String.IsNullOrEmpty(rowIdentifier))
{
Global.AddMessage(rtbCount, rowIndex);
Faraconesh.BusinessFramework.Common.Organization
organization = new Faraconesh.BusinessFramework.Common.Organization();
organization.Name = excelUtility.ReadCell(rowIndex,
startingCol + nameCol);
organization.OrgId = excelUtility.ReadCell(rowIndex,
startingCol + orgIdCol);
organization.Tel = excelUtility.ReadCell(rowIndex,
startingCol + telCol);
organization.Fax = excelUtility.ReadCell(rowIndex,
startingCol + faxCol);
organization.Address = excelUtility.ReadCell(rowIndex,
startingCol + addressCol);

if (result.ContainsKey(rowIdentifier))
throw new Exception("duplicate row number");
else
{
organization.RuntimeState =
Faraconesh.ApplicationFramework.Common.RuntimeState.Added;

Faraconesh.BusinessFramework.Common.CommonService.Instance.UpdateOrganization(organization);
result.Add(rowIdentifier, organization);
}
rowIdentifier = excelUtility.ReadCell(++rowIndex,
startingCol);
Application.DoEvents();
}
return result;
}

internal static void AddMessage(RichTextBox rtb, int intParam)
{
AddMessage(rtb, intParam.ToString());
}

internal static void AddMessage(RichTextBox rtb, string
stringParam)
{
const int maxLength = 4;
rtb.Text += stringParam + "\n";
if (rtb.Lines.Length > maxLength)
{
string[] oldLines = rtb.Lines;
string[] newLines = new string[maxLength];
for (int i = 0; i < maxLength; i++)
newLines = oldLines[i + 1];
rtb.Lines = newLines;
}
}
}



thanks,
afshar
 
Back
Top