|
Because you don't want to expose a public member that other classes can put a lock on. It could lead to a deadlock, as with this example (you will never see "Done"):
public class MyClass
{
private Action<MyClass> action;
public MyClass(Action<MyClass> action)
{
this.action = action;
}
public void DoStuff()
{
MessageBox.Show("Started");
lock (this) { action(this); }
MessageBox.Show("Done");
}
}
(new MyClass(delegate(MyClass instance) {
var t = new System.Threading.Thread(delegate()
{
lock (instance) { }
});
t.Start();
t.Join();
})).DoStuff();
Martin Fowler wrote: Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
|
|
|
|
|
From the documentation[^]:
"
In general, avoid locking on a public type, or instances beyond your code's control. The common constructs lock (this), lock (typeof (MyType)), and lock ("myLock") violate this guideline:
lock (this) is a problem if the instance can be accessed publicly.
lock (typeof (MyType)) is a problem if MyType is publicly accessible.
lock(“myLock”) is a problem since any other code in the process using the same string, will share the same lock.
Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.
"
|
|
|
|
|
First don't use string literals as a locking mechanism. Basically it doesn't work the way you think it does (regardless of all other issues.)
Second the semantics of your examples are different. In one case you are using a class level (static) and in the other you are using a instance level lock.
|
|
|
|
|
First don't use string literals as a locking mechanism. Basically it doesn't work the way you think it does
Please elaborate and explain
CodingYoshi
Artificial Intelligence is no match for Human Stupidity.
|
|
|
|
|
String literals are global to the application.
Thus the following represents one locking object instance regardless of the number of class instances for either class. (And the examples specifically do not use static.)
class A
{
private String lockObject = "a";
public void Doit()
{
lock(lockObject) {...}
}
}
class B
{
private String lockObject = "a";
public void Doit()
{
lock(lockObject) {...}
}
}
|
|
|
|
|
What is the best way to make application Multi Language ?
|
|
|
|
|
|
|
That's kinda what I said.
|
|
|
|
|
|
Find all "messages" shown to the user (in message boxes, labels, forms, ...). Give short names to those messages, put both the short name and the original text into a resource file, then grab the text from the resource file for the "short name" where ever something is to be shown to the user. The resource file can be translated by specialized companies. The entries there look something like:
<data name="IndentDlg_btnOK_Text" xml:space="preserve">
<value>&OK</value>
</data>
|
|
|
|
|
hi
how to convert my C# winform program to service program ?
i need that my program will work like service (like listener)
thanks in advance
|
|
|
|
|
|
The best way is to start a new Windows service project and move the code from the winforms app to the servuice app. In case you weren't aware, a windows service can't have any forms in it.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
I add an ocx control on my form (right click on toolbox->choose item ->select special .ocx->add it on form) and work correctly. then i make setup for my project and add .ocx file to my setup project but when i install setup on destination computer this .ocx control doesn't work
why ?
|
|
|
|
|
Try registering it with regsvr32 and see if that cures it.
Regards,
Rob Philpott.
|
|
|
|
|
Is it correct : Regsvr32 /s /u Application.StartupPath+@"\dsoframer.ocx"
|
|
|
|
|
No. the /u flag uninstalls it. Just run it without any flags.
Regards,
Rob Philpott.
|
|
|
|
|
Has the target got all the dependencies installed as well?
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Hello friends,
I am Navigating to a page using InternetExplorer object,
and I am using BeforeNavigate() event handler. INside BeforeNavigate I want to add one more element in POST data,here here is my code :
protected void onBeforeNavigate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
if (PostData != null)
{
string addend = "&PageSize=200";
byte[] b = PostData as byte[];
string post = Encoding.ASCII.GetString(b);
Console.WriteLine(post.Trim() + addend);
post = post + addend;
b = Encoding.ASCII.GetBytes(post);
PostData = null;
PostData = b as object;
}
}
But this is not working Post data remains the same as it was before (without editing),Iverified it by HttpAnalyzer tool.
Please help me out.
Thanks.
|
|
|
|
|
I have a central panasonic phone (KX-TES824BX) and now i wanna to get the info of my call log via RS232c cable connected to this central phone. witch dll i should use for this purpose.
So plz help me .
Thanks!
|
|
|
|
|
1. this is not a C# question
2. you need to look at the documentation for the phone
|
|
|
|
|
I want to validate an XML file through a schema.
="1.0"="utf-8"
<Units>
<Unt Text="stuks" Service="False" Value="0" />
<Unit Text="km" Service="True" Value="1" />
</Units>
="1.0"="utf-8"
<xs:schema id="Unit" targetNamespace="http://tempuri.org/Unit.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/Unit.xsd"
xmlns:mstns="http://tempuri.org/Unit.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="UnitType">
<xs:attribute name="Text" type="xs:string" use="required" />
<xs:attribute name="Service" type="xs:boolean" use="required" />
<xs:attribute name="Value" type="xs:byte" use="required" />
</xs:complexType>
<xs:element name="Units">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="Unit" type="UnitType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I use the following code :
Settings = new XmlReaderSettings();
Settings.ValidationType = ValidationType.Schema;
Settings.Schemas.Add("http://tempuri.org/Unit.xsd", Directory.GetCurrentDirectory() + strXmlSchema);
Settings.ValidationEventHandler += new ValidationEventHandler(OnSchemaValidationError);
using (XmlReader Reader = XmlReader.Create(Directory.GetCurrentDirectory() + strXmlFile, Settings))
{
if (Reader != null)
{
XmlDocument Doc = new XmlDocument();
Doc.Load(Reader);
Reader.Close();
:
}
}
My handler OnSchemaValidationError never gets called, even when
there is clearly a mistake in my XML. Why ??
|
|
|
|
|
I beleive you have to read the entire file in order for it to validate
try this
Settings = new XmlReaderSettings();
Settings.ValidationType = ValidationType.Schema;
Settings.Schemas.Add("http://tempuri.org/Unit.xsd", Directory.GetCurrentDirectory() + strXmlSchema);
Settings.ValidationEventHandler += new ValidationEventHandler(OnSchemaValidationError);
using (XmlReader Reader = XmlReader.Create(Directory.GetCurrentDirectory() + strXmlFile, Settings))
{
if (Reader != null)
{
while(Reader.Read()){}
}
}
|
|
|
|
|
Our servers have 2 physical network cards teamed together through dynamic link aggregation to create a virtual NIC.
Using WMI with Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration, I am able to see the virtual connection, virtual connections IP and MAC, I can see the first NIC's MAC address, but cannot see the second NIC's MAC address. WMI actually lists the second NIC's MAC address as the first NIC's MAC address for some reason? I can see it when I physically remote into the box and check the configuration settings for the adapter. It lists it as "Permanent Ethernet Address", but it is the MAC address.
Does anyone know how to remotely get both MAC addresses for the physical connections (Local Area Connection 1 and 2) and the IP address of the virtual connection (Dynamic Link Aggregation Team Connection 01) in C sharp?
|
|
|
|