|
If you want to refresh the page, You can Try this.
under the function of ComboBoxAddData() call Form1_Load(null,null). Now You can bind the your ComboBox will null values.
Plz check it.
|
|
|
|
|
So as mentioned, I'm trying to make a list of people connected but you have to first add that person, having done that, that person will appear in your listbox.
And another question, is to make a private chat, using the Network API.
The network api dll that I use is this:
Click here to download.
Thank you all.
|
|
|
|
|
|
After reading some article on performance and reading some of the .NET source code I am trying to reduce memory allocation and boxing as much as possible. Particularly in methods which are going to be called repetitively.
Now I know that foreach (var item in collection) works just as well and is more readable that
for (int i = 0; i < collection.Count; i++).
But I wonder foreach callGetEnumerator() which must return some new ThatEnumerator() which allocate an object!
So, a couple of nested foreach, bang, allocation, then GC, then slow down!
Any comments on that?
|
|
|
|
|
Any foreach loop has two extra variables on the stack, so while running foreach loop takes more memory and may be a bit slower...
The GetEnumerator problem is exist only if your enumerator is a class. In the .NET System.Collections.Generic namespace all collections (List<T>, Dictionary<TKey, TValue>)) implementing enumerator as a structure so it does not allocate memory on the heap, but returns a reference to an existing object...
If you want to reduce the memory peak and GC runs, try to use standard collections or build yours along the same line...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
modified 14-Jun-14 16:20pm.
|
|
|
|
|
Seems like part of your post got eaten
|
|
|
|
|
Thank you - fixed it...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Don't sweat the small stuff. I never worry about Garbage Collection; that's the point of having it.
I prefer to use for when I can, but I'm old-school that way.
Super Lloyd wrote: nested foreach
I don't think I've ever needed that.
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
I am not too worried..
But I am writing custom Panel (for WPF) which will be in hierarchical resizable items controls...
And they will do multi pass layout too...
Hence I am going to do a lot of recursive call on every mouse move events...
So far it's very quick and I am not really worried!
However I can't help to see how, in the .NET source code, they use struct as much as possible in such case and even use an internal BooleanBoxes to optimize boxing away for DependencyProperty(ies)...
And I though if I could reduce boxing and memory allocation in my panels, that would be nice too!
And I noticed the foreach => GetEnumerator() and was wondering about it!
It's not micro optimization, as this is a process on its own which has no link with anything else. But it will be a process that will be called regularly, why not spend 1 more hour to make it better?
At any rate.. I am using for loop now and only struct in my layout code and I reuse the same array, when I need one!
So memory wise it's good enough!
|
|
|
|
|
I think I'd be more concerned about the recursion.
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
Well, it can't be helped!
RibbonBars contains RibbonGroups which contains RibbonItems which contains Header!
(And MenuItems contains sub MenuItems which contains Header too)
Yeah I am making my own Ribbon library! But wait, there is a twist, it's called... MenuRibbon! And most ItemsControl in it have no particular container type! :P
|
|
|
|
|
Use the for loop .
I rarely use the foreach, because despite your personal feeling of being more readable, I find it less readable. (especially in the sense of what it is doing).
|
|
|
|
|
Using it now!
BTW, in some structure (say linked list for example) it is not really practical to use the for loop!
|
|
|
|
|
I know. I occasionally use the foreach too
|
|
|
|
|
Foreach allocates an enumerator and calls MoveNext and Current multiple times. For allocates a register variable (or whatever those are called in .Net) and also calls the property accessor Count (and, presumably, the indexer inside the loop) multiple times. The memory hit from the class reference against an integer variable is so small you shouldn't care, and the execution time will be very similar.
|
|
|
|
|
Super Lloyd wrote: So, a couple of nested foreach, bang, allocation, then GC, then slow down!
Presumably you know this because you actually measured it using realistic (production) data using a realistic (production) scenario.
If that isn't true then this exercise is pointless.
If it is true then the first and very best way to increase performance is to attempt to re-evaluate the algorithm itself. Thus instead of trying to make for loop faster try to eliminate all the for loops entirely.
|
|
|
|
|
I totally agree with this, what you're attempting sounds like premature optimisation and doing so without knowing the true bottle necks is usually a great big waste of time.
|
|
|
|
|
Hi,
I am using the following for() to loop through a datagrid to save to a database. My problem here is: when looping, I am getting all records correct except the last record which I am getting a blank cell value for it. what's wrong with my for() please?
for (int i = 0; i <= gridContacts.RowCount; i++)
{
sql_command = new MySqlCommand("sp_add_new_employee_contact", sql_connection);
sql_command.CommandType = CommandType.StoredProcedure;
sql_command.CommandTimeout = Convert.ToInt32(string_encryptor.DecryptString(xmlClass.read_xml_value("HRMS\\HRMS", "CommandTimeOut"), "JassimRahma@731004167"));
sql_command.Parameters.AddWithValue("param_employee_id", employee_id).MySqlDbType = MySqlDbType.Int32;
sql_command.Parameters.AddWithValue("param_contact_category", Convert.ToInt32(gridContacts.GetRowCellValue(i, "contact_category"))).MySqlDbType = MySqlDbType.Int32;
sql_command.Parameters.AddWithValue("param_contact_details", Convert.ToString(gridContacts.GetRowCellValue(i, "contact_details"))).MySqlDbType = MySqlDbType.VarChar;
sql_command.Parameters.AddWithValue("param_contact_description", Convert.ToString(gridContacts.GetRowCellValue(i, "contact_description"))).MySqlDbType = MySqlDbType.VarChar;
MessageBox.Show(Convert.ToString(gridContacts.GetRowCellValue(i, "contact_details")));
int result_rows = sql_command.ExecuteNonQuery();
}
Thanks,
Jassim
Technology News @ www.JassimRahma.com
|
|
|
|
|
Maybe
i < gridContacts.RowCount
?
Cheers
|
|
|
|
|
Why are you recreating the same MySqlCommand and parameters objects every trip through the loop? Wouldn't it better to just create them ONCE, before the loop, and then just populate the parameters and execute the command inside the loop?
|
|
|
|
|
Hear! Hear!
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
There is a lot of bad in that code. Others have pointed out some.
But also...
0) Don't bother trying to set the MySqlDbType of the Parameters -- the framework infers it from the Value.
1) Don't use Convert -- casting and Parsing is more efficient
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
Hello Jassim,
It is because Indexing start from 0 not from 1.
Look,
Your gridContacts.RowCount will return 10, if you have 10 rows and your for loop will be executed 11 times. In grid, there are only 10 records, so last record will be blank line.
So,
start index from i = 1 or use i < gridContacts.RowCount
But by looking at your code, you should use i < gridContacts.RowCount. Otherwise you have to change some of your code too.
Thanks... Have Fun
|
|
|
|
|
There are two ways of using this. To make the correction on your code, it should be,
for (int i = 0; i < gridContacts.RowCount; i++)
{
}
Alternative way is to try with For each, which i prefer the best suit for Datagrid,
foreach (DataGridRow drv in gridContacts.Items)
{
}
|
|
|
|
|
Correction in For Loop-
for (int i = 0; i < gridContacts.RowCount; i++)
|
|
|
|