Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi...
I have a methode in c# but it works very slowly,,,how I can speed it up?!

it's my code:

C#
private void button1_Click(object sender, EventArgs e)
        {
                        richTextBox1.Font = new Font("Scheherazade", 12, FontStyle.Bold);
            richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
            int int1 = int.Parse(textBox1.Text);
            try
            {
                foreach (Database24DataSet._Quran_simple___1Row row in qs.GetData())
                    if ((int)qs.selectsurrah(int1) == row.Surrah && (int)qs.selectsurrah(int1) < row.Surrah + 1)
                    {
                        richTextBox1.Text = richTextBox1.Text + "\n" + row.Arabic;
                        Application.DoEvents();
                    }
            }
            catch { }
        }
    }
}



for example: selectsurrah is a query,I enter a number in a textbox and it select some record and add into richtextbox
:

SQL
SELECT        Surrah
FROM            [Quran-simple - 1]
WHERE        (Surrah = ?)




help me!
thanks...
Posted
Updated 27-Aug-12 4:11am
v2
Comments
[no name] 27-Aug-12 10:14am    
Help with what? Profile your code and find out where the bottlenecks are. How, exactly, are we supposed to know what you have in your database? Ot how many rows your are getting back?
amir.nazarizadeh 27-Aug-12 10:21am    
it just works slowly,,,my database has 4 cols and maybe 7000 recs,,,maybe I cast every codes,,,

Implicit conversion is slow, Try with Explicit conversion via .NET Class Convert.
Set your Font properties on properties windows in design time of your richtextboxcontrol.

This line of code is called twice for every row in your collection, so you will have a very slow performance. So, here is the Main problem of your code.
C#
if ((int)qs.selectsurrah(int1) == row.Surrah && (int)qs.selectsurrah(int1) < row.Surrah + 1)

Try this one:
C#
private void button1_Click(object sender, EventArgs e)
{
    int int1 = 0;
    int.TryParse(textBox1.Text,out int1);

    int int_from_select = qs.selectsurrah(int1);

    foreach(Database24DataSet._Quran_simple___1Row row in qs.GetData())
    {
        if (row.Surrah.Equals(int_from_select) && int_from_select <(row.Surrah+1)) //Prevents to call a SELECT statement for each If
        {
            richTextBox1.Text = richTextBox1.Text + "\n" + row.Arabic;
            Application.DoEvents();
        }   
    }
}


Hope it helps.
 
Share this answer
 
v2
Comments
amir.nazarizadeh 27-Aug-12 10:31am    
that's nice,,,very very good,I tested it,,,it works very good,,,exellent,,thanks.
Christian Amado 27-Aug-12 10:33am    
Vote up or Mark as solution to helps others people. Nice to help you Amir.
shaikh-adil 27-Aug-12 11:33am    
awsome code
+5
Christian Amado 27-Aug-12 11:37am    
Thank you =)
As Wes says, it is very difficult to work out what is slow when we can't run it.
But, there are a few things you should do:
1) Never call Application.DoEvents - if the process is that slow, then move it into a background worker thread instead.
2) Don't concatenate strings: use a StringBuilder instead. Each time you do string1 = string2 + string3, you allocate a new area of memory big enough to hold string2 and string3 then copy both strings into it. If you have many rows, then this can be a very slow process - use a StringBuilder which only allocates new memory when it needs to.

Look at the Stopwatch class[^] - it can give you an idea what it taking time, and at least give you something to measure your improvemnets against.
 
Share this answer
 
Comments
amir.nazarizadeh 27-Aug-12 10:35am    
yeah,,,ok that was very good.
thanks.
shaikh-adil 27-Aug-12 11:34am    
nice answer griff
:)
If there is a lot of text to be displayed then even this :
richTextBox1.Text = richTextBox1.Text + "\n" + row.Arabic;

is slower than this:
richTextBox1.Text = richTextBox1.Text + string.format("\n{0}",row.Arabic);


Or even using a stringbuilder and only updating the GUI say every 100ms is better.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900