Dzien dobry,
Mam w aplikacji RichTexBox ktory zaiwera okolo 10tys. lini, potrzebuje w liniach ktore zawieraja wyszukiwany text zmienic kolor. Niestety moja procedura dziala bardzo wolno ;(.
Czy jest jakas metoda szybszego wyszukiwania tekstu w Richtextboxie? Wyszukiwanie I kolorowanie wykonywane jest w osobnym watku w Thread pool co tym bardziej nie pomaga, a I tak spowalnia dzialanie na GUI. Przetwarza okolo 1000lini na minute.
rtbProtocol to Rich box w GUI
private void UpdateProtocolSetup(object sender, EventArgs e)
{
Task.Run(() => {
int i = 0;
int imax = (int)this.Invoke(new Func<int>(() => rtbProtocol.Lines.Length));
for (i = 0; i < imax; i++)
{
SetText(label15, i.ToString());
bool hlExce = (bool)this.Invoke(new Func<bool>(() => (rtbProtocol.Lines[i].Contains("EXCE") & cbxExceProtocol.Checked)));
bool hlSql = (bool)this.Invoke(new Func<bool>(() => (rtbProtocol.Lines[i].Contains("SQL") & cbxSqlProtocol.Checked)));
bool hlOpc = (bool)this.Invoke(new Func<bool>(() => (rtbProtocol.Lines[i].Contains("OPC") & cbxOpcProtocol.Checked)));
bool hlCfg = (bool)this.Invoke(new Func<bool>(() => (rtbProtocol.Lines[i].Contains("CFG") & cbxCfgProtocol.Checked)));
bool hlPmon = (bool)this.Invoke(new Func<bool>(() => (rtbProtocol.Lines[i].Contains("PMON") & cbxPmonProtocol.Checked)));
bool hlDem = (bool)this.Invoke(new Func<bool>(() => (rtbProtocol.Lines[i].Contains("DEM") & cbxDemProtocol.Checked)));
bool hl = hlSql | hlOpc | hlCfg | hlPmon | hlDem;
if (hl | hlExce)
{
int start = (int)this.Invoke(new Func<int>(() => rtbProtocol.GetFirstCharIndexFromLine(i)));
int end = (int)this.Invoke(new Func<int>(() => rtbProtocol.Lines[i].Length));
SelectRtb(rtbProtocol, start, end);
if (hlExce)
// rtbProtocol.SelectionColor = Color.Red;
SelectionColorRtb(rtbProtocol, Color.Red);
else
//rtbProtocol.SelectionColor = Color.BlueViolet;
SelectionColorRtb(rtbProtocol, Color.BlueViolet);
//rtbProtocol.SelectionLength = 0;
SelectionLengthRtb(rtbProtocol, 0);
}
//SetText(label15, i.ToString());
}
});
}
private void SetText(Control sender, string aText)
{
if (sender.InvokeRequired)
{
Invoke((Action<Control, string>) SetText, sender, aText);
return;
}
sender.Text = aText;
}
private void SelectionColorRtb(RichTextBox aRtb, Color aColor)
{
if (aRtb.InvokeRequired)
{
Invoke((Action<RichTextBox, Color>)SelectionColorRtb, aRtb, aColor);
return;
}
aRtb.SelectionColor = aColor;
}
private void SelectionLengthRtb(RichTextBox aRtb, int aSelectionLength)
{
if (aRtb.InvokeRequired)
{
Invoke((Action<RichTextBox, int>)SelectionLengthRtb, aRtb, aSelectionLength);
return;
}
aRtb.SelectionLength = aSelectionLength;
}
private void SelectRtb(RichTextBox aRtb, int aStart, int aEnd)
{
if (aRtb.InvokeRequired)
{
Invoke((Action<RichTextBox, int, int>)SelectRtb, aRtb, aStart, aEnd);
return;
}
aRtb.Select(aStart, aEnd);
}
Przetwarza okolo 1000lini na minute.
- ło jezu, Ty to szesnastomegahercowej Atmedze odpalasz?