|
|
I have a multithreaded app which creates several objects. Inside the object class is something like:
public ArrayList Associations
{
get
{
if(this.assocs == null)
{
lock(this)
{
if(this.assocs == null)
{
this.assocs = new ArrayList(500);
return this.assocs;
}
}
}
else return this.assocs;
}
}
Is this kind of lazy initialization safe to do in a multithreaded environment?
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Cops & Robbers
Judah Himango
|
|
|
|
|
I'd use single locking:
<pre>
public ArrayList Associations
{
get
{
lock(this)
{
if(this.assocs == null)
{
this.assocs = new ArrayList(500);
return this.assocs;
}
return this.assocs;
}
}
}
</pre>
unless performance testing shows you need to do double locking, then you need to add a memory barrier,
<pre>
get
{
// use lazy initialization to create the associations:
if(this.assocs == null)
{
lock(this)
{
if(this.assocs == null)
{
ArrayList result = new ArrayList(500);
System.Threading.Thread.MemoryBarrier();
this.assocs = result;
return this.assocs;
}
}
}
return this.assocs;
}
</pre>
You can also make assocs volatile instead and use the double locking.
<a href = "http://blogs.msdn.com/brada/archive/2004/05/12/130935.aspx" rel="nofollow">http://blogs.msdn.com/brada/archive/2004/05/12/130935.aspx</a>[<a href = "http://blogs.msdn.com/brada/archive/2004/05/12/130935.aspx" target = "_blank" rel="nofollow">^</a>]
<hr><div style="font-size:xx-small;font-family:arial;color:grey;"><font size=1> I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon</font></div>
|
|
|
|
|
I was concentrating on your lazy initalization question so much, I missed the bigger issue. You can't just expose a dynamic array list to multiple threads.
Use
this.assocs = ArrayList.Synchronized(new ArrayList(500));
instead.
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
Actually, your other post answered the question; I used ArrayList as an example only, my actual code uses a custom List<MyObject>.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Cops & Robbers
Judah Himango
|
|
|
|
|
Hi, i made a windows service, i added the service installer, the service process installer, etc..Then, i did the installer and installed the windows service. It appears in my add/remove programs window, but it DOESNT appear in my Services window, so i cant start it or stop it, or whatever..Does anybody know why? what am i missing? thanks
|
|
|
|
|
Did you make sure to put the [RunInstaller(true)] attribute above your service installer class? Also, make sure you close and re-open the services window, since it will cache and not notice things sometimes.
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Does someone have experience reading WM_INPUT info? I already know I need to use user32.dll, I only have no idea HOW. There are masses of tutorials in c or c++ on this subject, but just a few in C#. I'm a programmer newbe, so I like code with some explanation
Thanks in advance.
|
|
|
|
|
Can somebody tell me the best way to implement the logic similar to SQL Composite Key Constraint.
|
|
|
|
|
Where do you want to implement this?
---
b { font-weight: normal; }
|
|
|
|
|
I have an array of objects of a class having 10 fields. The first 5 fields should behave like composite keys of a sql table. So only objects conforming to the constraint can be added.
I know that this can be achieved using DataSets and DataRelations. I dont want to use it.
I just need a simple logic to implement this in an ArrayList.
|
|
|
|
|
Slow method:
Loop through all the records and compare the values in the fields to the new values you want to add.
Fast method:
Create a hash value for the fields and store in the object. Create a hash value for the new values and loop through the records and look for the hash value.
Even faster method:
Use a hash table instead of an array, create a composite value of the fields and use as key in the hash table. Create a composite value of the new values and check if it exists in the hash table.
---
b { font-weight: normal; }
|
|
|
|
|
Hi community,
i cteate an application on win xp prof. with frameworks 1.0,1.1 and 2.0 Beta, and there r no errors on machines with framework 2.0, but only if i try to install them to an machine with framework 1.1 or 1.0 i got an error that i need framework version 2.0! How can i target an framework version that is installed on the current machine??? is this posible??? i use VS .Net 2005! i try to change the version in the setup->lauch conditons->version = 1.1.4322 but they dont work?! when i change all references from 2.0 to 1.1 i recive an error like this:
"System.BadImageFormatException: The format of the file 'Installer.exe' is invalid"
Any suggestions for me?
Thanx for help!
mirso
-- modified at 8:24 Wednesday 7th September, 2005
|
|
|
|
|
I have a textbox that needs to accept the format: 00:00:00
I would like for the format to be "Locked." Therefore, forcing the user to keep the values in this format. For example, when the textbox shows up, it automatically displays 00:00:00. And the user MUST put values in each place or leave them "0." I took a look at the DateTime method. However, it doesn't seem to do what I am looking for. Also, all values entered can only be 0 - 9. Thanks in advance.
D
|
|
|
|
|
What you are looking for is a "masked edit control". Searching here at CodeProject or on Google will list many of them!
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
I can aso use rgular esxpressions whit the System.Text.RegularExpressions.RegEx.Ismatch method.
Here is one string pattern to use: "^(20|21|22|23|[01]\d|\d)(([:.][0-5]\d){1,2})$"
You can see more at: http://www.regexlib.com/DisplayPatterns.aspx[^]
Happy coding!
La realidad no es más que impulsos eléctricos del cerebro - Morpheus
|
|
|
|
|
Hello,
Is there a way to create a working installer for a snapin? I've created a snapin but I can't create a working installer Has one of you guys ever delt with this problem? Is there a solution?
10x guys
Larry
--
|
|
|
|
|
Hello,
I have a question regarding the inheritance of a sealed class. I would really want to know if it is possible inherit from FolderBrowserDialog. I want to extend the FolderBrowserDialog to do some checkings of the selected folder and to enable/disable the OK button. How can I do that in C#?
Larry
|
|
|
|
|
Hai
Is there any way to find the line number & Source code that cause excpetion.
i mean in the code i need to get these things in seperate variables.
string linenumber = 'line number'
string expCode = ' code that leads to exception'
Thanks and Regards
|
|
|
|
|
You can get this information from Exception.ToString() if you look to the end of the string you will see somthing like form1.cs:line 524 so to get line number you can write code in your exception handling code somthig like
catch(Exception ex)
{
string strExc=ex.ToString();
int pos=strExc.IndexOf(":line",0);
if(pos != -1)
{
MessageBox.Show(strExc.Substring(pos+5,strExc.Length-(pos+5)));
}
}
Note: in relase mode this inoformation will not generated to enable it in your project Build configration check Generate debugging information look at Printing Line Number with an exception in Release Mode[^] for more informaton
MCAD
-- modified at 13:04 Wednesday 7th September, 2005
|
|
|
|
|
Hi All,
I was just wondering how to invoke a process under different security context with the specified credentials over network only, I used P/Invoke to call CreatProcessWithLogonW but since I had to use it with ASP.NET application p/Invoke was failing though It was working fine with Desktop application (Probably due to some security problems of IIS), I came to know that in .net2.0 things are pretty simple and I need to call only Static member Start of Process class by passing credentials but this is not invoking process instead I am getting error logon failure, I suppose this problem is becuase I don't know how to set logon flags which we could set in createprocesswithlogonw function..Can anyone tell me how to start a process using the specified credentials on the network only in .net2.0.
Regards,
Ruchir Dhar Dwivedi
Software Engineer
5th floor Marisoft, Cybage Software Pvt. Ltd., Kalyani Nagar, PUNE - 4 1 1 0 0 6, I N D I A
Phone(O) : 91-20-56041700,91-20-56044700 Extn 830 | AIM : RuchirAIM | cell : 09890939702
mail to : ruchird@cybage.com | www.cybage.com
|
|
|
|
|
Sorry guy, im new in c#
Im using vs.net
Is there a way to update the input of a text box from a class that not in the main form1.cs? my additional class is suppose to update a status text box when a msg is sent.. any hint guys?
I suck at coding and only familar with C.. now trying out c#
string test=("listening:");
Form1.StatusText= test;//string.Copy("Listening..:");
|
|
|
|
|
It is possible to update the text of a text box in the main form from another class in the application. Proceed something like this…
string test = "Listening:";
Form currentForm = Form.ActiveForm;
((Form1)currentForm).StatusText.Text = test;
Regards,
Ashok Dhamija
_____________________________
Padam Technologies
-- modified at 5:12 Wednesday 7th September, 2005
|
|
|
|
|
C:\Documents and Settings\Desktop\C# CLASS\ClientWithDialog\cApplication.cs(29): The name 'currentForm' does not exist in the class or namespace 'ClientWithDialog.cApplication'
C:\Documents and Settings\Desktop\C# CLASS\ClientWithDialog\cApplication.cs(28): The type or namespace name 'Form' could not be found (are you missing a using directive or an assembly reference?)
This are my error..
The situtation is this.. i got a console program. which im tryin to rewrite into a application base one.
it a simple tcp/ip program
where by the StatusText textbox is suppose to change according to what the class throw at..
example when connected..( procedure in class)
it suppose to update the status text box in the form i created.
hope for some hint
|
|
|
|
|
As you had not mentioned that it is a console application and as you had referred to your main Form as Form1 in your sample code, I had presumed that you are using a Windows Forms application. In a forms application, the error mentioned by you is due to the missing "using" statement. Use the following "using" statement at the top of the class coding:
using System.Windows.Forms;
But, in the situation described by you, and to the extent I could understand your problem, you should have the reference to the instance of Form1 on which the "StatusText" textbox lies, and then may simply use code like this:
string test = "Listening:";
Form1.StatusText.Text = test;
Hope it works.
Regards,
Ashok Dhamija
_____________________________
Padam Technologies
|
|
|
|