|
Luc Pattyn wrote: So typical code sequence is: test, lock, test again, do, unlock
Thanks. So will it be like this ?
private static object locker = new object();
private static Configuration _configuration;
public static Configuration Current
{
get
{
if(_configuration == null)
{
lock(locker){
if(_configuration == null){
_configuration = LoadConfiguration();
}
}
}
return _configuration;
}
} Also I heard that the double checking has some performance problems and it's not a recommended practice. Is that correct ? Also I am thinking about an alternative approach like this,
private static readonly Configuration _configuration = LoadConfiguration();
public static Configuration Current
{
get
{
return _configuration;
}
} How about this ? Will readonly approach makes any problems in multi threaded environment ?
|
|
|
|
|
Hi,
your first snippet is OK.
double test is very cheap, and much cheaper than always locking, hence no
performance issues at all.
Your eager execution (second snippet) has one advantage:
it is simpler, less code.
and two disadvantages:
1. it always does the load; that's OK if you always need it, which may or may
not be true;
2. it may execute sooner than required (e.g. by accessing something else in the
same class, hence class load and execution of static code), hence delaying an operation
the user might be waiting for.
It depends entirely on the app itself whether the eager code is fine or not.
I tend not to do eager stuff in the main thread, so overall my preference is
doing this kind of initialization in a background thread, unless it is really needed
early on in the app.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Luc Pattyn wrote: it always does the load
Means it calls the LoadConfiguration method each time when I access the Current property ?
Luc Pattyn wrote: it may execute sooner than required (e.g. by accessing something else in the
same class, hence class load and execution of static code)
Ya, this is correct. But this class contains only this property.
Luc Pattyn wrote: so overall my preference is
doing this kind of initialization in a background thread,
Do you mean the readonly kind or the first one ?
Thanks for your help.
|
|
|
|
|
N a v a n e e t h wrote: Means it calls the LoadConfiguration method each time when I access the Current property ?
No, obviously not. The getter only returns the member's value, no more, no less.
But the first time you do anything with the class the static code gets executed,
so you perform LoadConfig even if you never get Current at all. That may or may not be
acceptable, up to you.
N a v a n e e t h wrote: Do you mean the readonly kind or the first one ?
The eager one, i.e. have a background thread do it at its own pace, without blocking
the main thread, but before it is really necessary.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
|
Hello,
I am creating a mobile 5 application using CF 3.5.
I have a combo box that keeps on firing on the selectionIndexChanged event. I need this event to get an item from the combo box. However, when the combo datasource is set it will fire this. I have resolved this issue with a simple bool test. See code below. However, I have noticed that the event often fires. Its hard to track down what is causing this to fire.
<br />
this.cboDataSourceSet = false;<br />
this.cboRecentNumbers.DataSource = this.bsRedialedNumbers;<br />
this.cboRecentNumbers.DisplayMember = "Name";<br />
this.cboRecentNumbers.ValueMember = "ID";<br />
this.cboDataSourceSet = true;<br />
<br />
private void cboRecentNumbers_SelectedIndexChanged(object sender, EventArgs e)<br />
{<br />
<br />
if (this.cboDataSourceSet)<br />
{<br />
}<br />
}<br />
I am thinking of 2 methods to solve this issue:
1) Is there a way to stop the event being fired or a way to track down the event to find what other conditions are causing it to fire?
2) Inherit from the combo box class and add your own SelectionChanged Event. (Can anyone point me in the right direction for doing something like this?).
I would perfer method 2 as I have never done anything like that before. So good for learning something new.
Many thanks for any suggestion and help with this,
Steve
|
|
|
|
|
I have come across something similar (or maybe even the same), IMO, your simple solution is adequate
If you want to inherit, you can try override OnSelectedIndexChanged. Calling the base implementation should fire the event.
|
|
|
|
|
Hi guys,
I have a scenirio where by i need create some circles with varying distance in the sides of a triangle on a Web Page where all the sides are equal.
I have the points for all the three vertex.
I know just because i am using C# for that this question does't belong here.
But please let me know if you have any idea on this..
Thank you
Thanks
Laddie
Kindly rate if the answer was helpful
|
|
|
|
|
You work out the ratio of width to height and use that to calculate the x for a given y, or vice versa.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi Christian,
Thank you for the reply. But it will work only if the line is straight.
You mean to say new x = (Width - n) Right..
But here it is a slanting line. Like the sides of a triangle.
Or may be i got you wrong ?
Thank you
Laddiw
Thanks
Laddie
Kindly rate if the answer was helpful
|
|
|
|
|
You got me wrong.
If x1=5, y1 = 10, x2 = 25, y2 = 50
then the ration is (25-5)/(50-10), or 20/40 or 1/2.
So, if you want to know the y position when x is 10, you subtract 5, to get to your base line. Then you multiply 5 by 2. Then you add 10 ( your starting y ). So, 10,20 is a point on the line.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Ok. Looks like it should work..Let me try implementing the same and will come back.
Thank you
Have a nice day.
Thanks
Laddie
Kindly rate if the answer was helpful
|
|
|
|
|
Hi chris,
I created a function something like this
public void pointX(int x1, int y1, int x2, int y2, int dis,out float newX,out float newY)
{
float ygrad = y2 - y1;
float xgrad = x2 - x1;
ygrad /= 100;
xgrad /= 100;
newY = (dis * ygrad) + y1;
newX = (dis * xgrad) + x1;
}
It is working fine...
The requirement was that i need to draw some circles depending on some percentage, whose center will be an offset from the side of a triangle, where offset is the percentage.
Any way thank you for that idea..
Have a nice day.
Thanks
Laddie
Kindly rate if the answer was helpful
|
|
|
|
|
I'm building a program in which the content (data, images etc) needs to be updated regularly by a non-programmer.
Is there any way I can update only the content but not the .exe ??
I've read about ClickOnce, but as far as I can see it will check for new version of the .exe
TIA
|
|
|
|
|
if it does, it's just going to find it's not changed, right ?
If it's just content, you can write your own FTP code to download it as needed.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Ok, let me make this clearer.
My program is going to read from an XML file which in turn contain links on local folder to image files. The updated one will be the XML & image files, not the .exe
All the files will be saved on the server, and ready to be 'pulled' from the clients when they check for update.
I guess FTP code is fine, but I need the mechanism so that the client know that there is new data to be updated.
Hope that clears it up
Thanks
|
|
|
|
|
I would assume one of two things
1 - you download a file via FTP that has a date of last update in it, and check that
2 - you write a webservice which wraps that stuff up a bit nicer, but basically does the same thing
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
DataSet DBset = new DataSet("Client_Number");<br />
DBCommand = new OleDbCommand("Select Client_Number from tbClient", DBConnection);<br />
OleDbDataAdapter mydb = new OleDbDataAdapter();<br />
mydb.SelectCommand = DBCommand;<br />
mydb.Fill(DBset);<br />
cmbClientID.BeginUpdate();<br />
cmbClientID.DataSource = DBset.Tables["Client_Number"];<br />
cmbClientID.DisplayMember = "Client_Number";<br />
cmbClientID.ValueMember = "Client_Number";<br />
cmbClientID.EndUpdate();
This is my code. It doesn't work correctly as I want.
Sorry, I have corrected that mistake.
modified on Wednesday, March 26, 2008 8:06 PM
|
|
|
|
|
And, what is it doing instead ? Is there anything in your dataset ?
I assume all the ???? are there for looks, so your boss can't work out that you're posting here ? The code doesn't look like this, right ?
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
I have corrected it. It's some non english symbols.
|
|
|
|
|
DataSet DBset = new DataSet("Client_Number");<br />
DBCommand = new OleDbCommand("Select Client_Number from tbClient", DBConnection);<br />
OleDbDataAdapter mydb = new OleDbDataAdapter();<br />
mydb.SelectCommand = DBCommand;<br />
mydb.Fill(DBset);<br />
cmbClientID.BeginUpdate();<br />
cmbClientID.DataSource = DBset.Tables["Client_Number"];<br />
cmbClientID.DisplayMember = "Client_Number";<br />
cmbClientID.ValueMember = "Client_Number";<br />
cmbClientID.EndUpdate();
Very difficult to understand here. Why nothing in my combobox cmbClientID????
Everything seems to be normal!??
|
|
|
|
|
cuongmits,
Your code looks fine. Check if the "DBset.Tables["Client_Number"]" has any rows via:
Trace.WriteLine("Client_Number Row Count" + DBset.Tables["Client_Number"].Rows.Count.ToString());
Regards,
Gareth.
|
|
|
|
|
Hi All,
I know this may seem trivial to many here but I've just implemented my first background worker thread with a meaningful GUI update in the shape of a progress bar ...
I thought in the light of so many dumb questions from me lately a bit of success should go reported as well ...
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
*grin* threading is always fun. And not in a good way. Congratulations
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Cheers fella ... its been a tad hairy already ... loads of testing to do to make sure It doesn't end up in thread hell!
I found this free ebook from jo Albahari really useful.
http://www.albahari.com/threading/[^]
I REALLY need to get a copy of his book - C# in a nutshell, total respect for the way he writes. So many techie books are written by people too close to the code if you get my meaning.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|