I am working in C#, winform and Mysql. (Visual Studio 2010).
In my project all menustrip items are invisable. At the login time its visible which menustrip item user rights. For that i invisible all menu strip,. And I code for visible the menustrip items.
But its take too much time. more then 5 minutes. So I have Few Questions.
- How I find this coding execution time?. (My Code Below)….
- Is any special tool is there?.
- How to Reduce the time with effective code?
private void menuActive(ToolStripItemCollection items) { hp.getConnStr(); MySqlConnection connection = new MySqlConnection(hp.myConnStr); MySqlCommand command = connection.CreateCommand(); MySqlDataReader Reader; command.CommandText = "select menu_key from mcs_menu_rights where userid ='"+userId+"'"; connection.Open(); Reader = command.ExecuteReader(); while (Reader.Read()) { foreach (ToolStripMenuItem item in items) { if (item.Name == Reader[0].ToString()) { item.Visible = true; } else { menuActive(item.DropDown.Items); } } } connection.Close(); }
You could profile, but there are some definite improvements I can see straight off:
Seperate out your recusive function away from the database query. This way you query the database once. Note that it’s not doing exactly the same, but I’m not sure what that table looks like so you’ll have to test it.