|
Does the function use shared data? Shared data includes member variables, static members of the current and other classes etc.. If not, your function is thread safe.
Because each thread has its own stack, as long as you use only local variables and the parameters*, your function should be thread safe.
*Beware of reference parameters though, it is possible that some other thread might change the object after passing it to your function.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hi all,
does anybody know what the default ApartmentState of a C# application is, if you do not set the [STAthread] attribute for your main method?
I have read different statements in the Internet. Some say that the default ApartmentState for a C# application is "ApartmentState.MTA", whereas for VB.NET it's "ApartmentState.STA".
Others say that the default state is "ApartmentState.Unknown".
Well, I tried it out myself using a simple C# app, but what I found was not really good:
Almost every computer had "ApartmentState.Unknown" as default, but unfortunately some had "ApartmentState.MTA".
What influences this behavior?
The reason why I'm asking is because I have an application which uses CoInitlizeSecurity and DragAndDrop.
The app starts like this:
public static int Main(string[] args)
{
System.Threading.Thread.CurrentThread.ApartmentState = ApartmentState.STA;
int nHresult = CoInitializeSecurity(IntPtr.Zero, -1, IntPtr.Zero, IntPtr.Zero, RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IMPERSONATE, IntPtr.Zero, 0, IntPtr.Zero);
...
}
This almost ever works. But on some computers that have MTA as default ApartmentState, I cannot reset the CurrentThread's ApartmentState to STA, because this can be set only once. The consequence is that "Drag and Drop Registration" fails, because Drag and Drop requires STA.
When I put the [STAThread] attribute to my main method, then the CurrentThread's ApartmentState is STA on every computer, but CoInitializeSecurity returns RPC_E_TOO_LATE.
Now is there something that I can do to solve this problem?
thanks in advance for your help!
-------------------------------------------
The light at the end of the tunnel has been switched off temporarily due to budget problems...
|
|
|
|
|
I haven't dealt with this problem myself, but this[^] article might help you out a little.
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.
|
|
|
|
|
All
I have an application which loads a large amount of data into a combobox, ~1000 entries.
Before my machine got updated with .NET CLR 1.0 SP3 the dialog with the combobox was displayed an immediately ready for use. Now with SP3 the dialog is locked out for ~2 seconds.
Using Compuware Dev Partner Performance Analysis the population of the combobox is the culprit.
The code access security permissions are being invoked and taking an inordinate amount of time on the Combobox.AddRange.
By denying the various permissions the UIPermission was identified as being invoked as follows.
System.Security.Permissions.UIPermission, mscorlib, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.
at System.Security.SecurityRuntime.FrameDescHelper(FrameSecurityDescriptor secDesc, IPermission demand, PermissionToken permToken)
at System.Security.CodeAccessSecurityEngine.Check(PermissionToken permToken, CodeAccessPermission demand, StackCrawlMark& stackMark, Int32 checkFrames, Int32
unrestrictedOverride)
at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
at System.Security.CodeAccessPermission.Demand()
at System.Windows.Forms.Control.FromHandle(IntPtr handle)
at DevExpress.XtraBars.MessageFilter.BarManagerMessageFilter.InternalPreFilterMessage(BarManager manager, Int32 Msg, IntPtr HWnd, IntPtr WParam, IntPtr
LParam)
at DevExpress.XtraBars.MessageFilter.BarManagerMessageFilter.DevExpress.Utils.Win.Hook.IHookController.InternalPreFilterMessage(Int32 Msg, IntPtr HWnd, IntPtr
WParam, IntPtr LParam)
at DevExpress.Utils.Win.Hook.HookManager.InternalPreFilterMessage(Int32 Msg, IntPtr HWnd, IntPtr WParam, IntPtr LParam)
at DevExpress.Utils.Win.Hook.HookManager.WndHook(Int32 ncode, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.SafeNativeMethods.GetWindowTextLength(IntPtr hWnd)
at System.Windows.Forms.Control.get_WindowText()
at System.Windows.Forms.ComboBox.NativeClear()
at System.Windows.Forms.ObjectCollection.ClearInternal()
at System.Windows.Forms.ObjectCollection.Clear()
However asserting the permission as follows did not resolve the issue,
[UIPermissionAttribute(SecurityAction.Assert, Unrestricted=true, Window=UIPermissionWindow.AllWindows)]
Has anyone else encountered this change in security behaviour, and know of any configuration change that can remedy the performance penalty now being incurred?
Regards
David
Hasta La Victoria Siempre!
|
|
|
|
|
Hi...
I have a richtextbox whit readonly enabled, but the user must type at the keyboard anyway, but when they do, there comes a beep from the copmuter! is there a way to disable that sound? (the sound also come when you press 'Enter' in a textbox)
Thanks...
QzRz
QzRz
|
|
|
|
|
You can prevent it by deriving from the TextBox class and overriding the OnKeyPress event. E.g.
public class MyTextBox : TextBox <br />
{ <br />
protected override void OnKeyPress(KeyPressEventArgs e) <br />
{ <br />
if(e.KeyChar == (char) 13) <br />
e.Handled = true; <br />
else <br />
base.OnKeyPress (e); <br />
} <br />
}
Regards,
Polis
Can you practice what you teach?
|
|
|
|
|
It look great... but... how do i use it...? :confused....
I have this..
private void richTextBoxText_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
e.Handled = true;
Tastatur_KeyDown(sender,e);
MarkColor((Keys)e.KeyCode,e);
}
The Tastatur_KeyDown and MarkColor is what i want to do when the user press a key!
Can you show me how to use your example on this...?
thanks!
QzRz
|
|
|
|
|
Instead of using the standard RichTextBox, create a new class that inherits from RichTextBox:
public class MyRichTextBox : RichTextBox
{
private void richTextBoxText_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
e.Handled = true;
Tastatur_KeyDown(sender,e);
MarkColor((Keys)e.KeyCode,e);
}
}
Use MyRichTextBox class instead of the standard RichTextBox.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: He has a funny face. And he's my son.
Judah Himango
|
|
|
|
|
Hey!
I have done my own stream with all the owerride methods.
My stream class is working against a simple Stream. the only extra is Events when data is written to the stream.
Anyway
I want to use StreamWriter to write rows to the stream, like this:
<br />
public void WriteRow(string data)
{<br />
try<br />
{<br />
StreamWriter sw;<br />
<br />
foreach (UnitStream unit in StreamCollection)
{<br />
if (unit.getUnitStream.CanWrite)<br />
{<br />
MemoryEventStream mES = unit.getUnitStream;
sw = new StreamWriter(mES); <br />
sw.WriteLine(data);
}<br />
}<br />
}<br />
catch (Exception ex)<br />
{<br />
addError("Error in " + this.ToString() + " - WriteRow : " + ex.Message);<br />
}<br />
}<br />
There is no data added to my stream, and i have set break points on write and writebyte in my own made streams to see if its this the StreamWriter is using, but it is´t?
How do StreamWriter work and way is ther not data written to my Stream?
BestRegards
SnowJim
|
|
|
|
|
Hi!
By using Reflector[^] you can look at the code in the FCL.
My first guess would be that you simply didn't flush the StreamWriter , so the internal buffer hasn't been transferred to your stream.
Regards,
mav
|
|
|
|
|
Hello C#-experts!
I'm trying and trying and trying but it wont work. Can someone give me an example?
What I've got is:
Domainname: testdomain
Username: administrator
password: 12345
group: string[] (containing groupnames)
from msdn.microsoft.com I've found something like:
DirectoryEntry group = new DirectoryEntry("LDAP://CN="+string[i]+",DC=Fabrikam,DC=COM");<br />
foreach(object dn in group.Properties["member"] )<br />
{<br />
Console.WriteLine(dn);<br />
}
But he keeps throwing exceptions like: domain not found etc.
Please help me!
|
|
|
|
|
There are a number of things to consider here. First of all, what format are the group names in? The display name of the group is different from the downlevel name (sAMAccountName) is different from the directory (or common) name (cn). You want the common name for what you're doing.
If you're checking the membeship of group X, in addition to checking the "member" property, you'll also have to include all users who have their primary group set to X. These users will not show up in the "member" property, but are still considered members of the group.
Also, to reliably get the "dc=fabrikam,dc=com" part of the distinguished name, you should be doing the following:
// Assuming you're logged into the domain that these groups are in:
DirectoryEntry rootDSE = new DirectoryEntry( "LDAP://rootDSE" ) )
string namingContext = rootDSE.Properties["configurationNamingContext"];
|
|
|
|
|
Thanks for the reply Steve. Now I finally know the correct "configurationnamingcontext".
But you say: "Assuming you're logged into the domain..." How do I log-in?
Now I try:
try<br />
{<br />
DirectoryEntry rootDSE = new DirectoryEntry( "LDAP://cu4it/rootDSE" );<br />
string namingContext = rootDSE.Properties <br />
["configurationNamingContext"].Value.ToString();<br />
<br />
DirectoryEntry group = new DirectoryEntry<br />
("LDAP://"+namingContext, "administrator","**********"); <br />
<br />
foreach(object dn in group.Properties["member"] )<br />
{<br />
Console.WriteLine(dn);<br />
}<br />
}
At the foreach he throws an exception: Can't find domain.
Oh and I'm assuming that I'm working with displaynames/commonnames from the groups and sAMAccountnames from the users. Also I need to get all the users that the group contains.
(sorry for my bad english 'cause I'm from The Netherlands)
|
|
|
|
|
Sounds like you have a problem with DNS then. In your rootDSE binding string, try specifying the full DNS name for that server or domain. Or you can also get the "dnsHostName" property from the rootDSE and bind explicitly to that when you bind to the group.
e.g.,
string dnsName = rootDSE.Properties["dnsHostName"].Value.Tostring();
string groupPath = string.format( "LDAP://{0}/{1},{2}", dnsName, groupName, namingContext );
DirectoryEntry group = new DirectoryEntry( groupPath );
If that doesn't work, then you *definately* have a problem with DNS.
|
|
|
|
|
Again, Thank you so much. I've got it (almost)working !
But like you said before, I only get one name. Even though I use a foreach-statement.
Here's my code: ("cu4it" is my domain-name")
string eigenstring = "LDAP://cu4it/CN="+strGroup+",CN=Users,DC=Cu4IT,DC=tld";<br />
DirectoryEntry ent = new DirectoryEntry(eigenstring);<br />
<br />
DirectorySearcher mySearcher = new DirectorySearcher(ent);<br />
<br />
SearchResultCollection coll = mySearcher.FindAll();<br />
foreach (SearchResult rs in coll)<br />
{<br />
ResultPropertyCollection resultPropColl = rs.Properties;<br />
foreach( Object memberColl in resultPropColl["member"])<br />
{<br />
DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://cu4it/"+ <br />
memberColl);<br />
System.DirectoryServices.PropertyCollection userProps = <br />
gpMemberEntry.Properties;<br />
object obVal = userProps["sAMAccountName"].Value;<br />
if (null != obVal)<br />
{<br />
slet.Add(obVal.ToString());<br />
}<br />
}<br />
}
What's your tip/trick to list all subscribed users to a group?
Greetingz
|
|
|
|
|
Hello ;
I'm programming a .net application and i'm using a matlab files ; on using this files (always in a threads) i receive a warning messages like "Warning matlab : Fatal error on start up" or " Failed java matlab services".
Please can u tell me the explication of such errors.
Thank's a lot
|
|
|
|
|
Hi all,
when i am executing the following code the, if the function invoked returning other than null, it is working fine. if it returns null, then it throws the object reference not found exception. please help.
string functionName ="GetMRN";
System.Reflection.MethodInfo methodinfo;
methodinfo = this.GetType().GetMethod(functionName);
objParamArr[0] = Param2;
objParamArr[1]=param2;
string strResult = methodinfo.Invoke(Invoke(this,objParamArr).ToString();
regards.
HAsan
Contact him at: hasansheik@hotmail.com
hasansheik@yahoo.co.in
hasansheik@lycos.com
|
|
|
|
|
|
Hi all,
when i am executing the following code the, if the function invoked returning other than null, it is working fine. if it returns null, then it throws the object reference not found exception. please help.
string functionName ="GetMRN";
System.Reflection.MethodInfo methodinfo;
methodinfo = this.GetType().GetMethod(functionName);
objParamArr[0] = Param2;
objParamArr[1]=param2;
string strResult = methodinfo.Invoke(Invoke(this,objParamArr).ToString();
Contact him at: hasansheik@hotmail.com
hasansheik@yahoo.co.in
hasansheik@lycos.com
|
|
|
|
|
I am writing console batch application which does intense processsing on bitmap. I would like to know the preffered way of dealing with Min and Max values, its good if I can save a microsecond as batch application runs for a week.
minValue = Math.Min ( val1, val2 )<br />
<br />
minValue = val1 < val2 ? val1 : val2;
Also is it possible to know which one is faster by examining metadata?
REgards.
MaulikCE
|
|
|
|
|
|
What about others, "double" and "float"? One more, is there any performance difference between IF ELSE and CONDITIONAL EXPRESSION?
|
|
|
|
|
|
Here is the authentic word, the second way (of using If-else) is almost 100% faster than the first method (of using Static method of Math). Here is how I arrived at the result.
Had a simple project with two buttons and two labels, with the following event-handlers for their click events:
private void button1_Click(object sender, System.EventArgs e)
{
int minValue;
int val1 = 5;
int val2 = 7;
DateTime time1 = DateTime.Now;
for(long i=0; i<1000000000; i++)
{
minValue = Math.Min ( val1, val2 );
}
DateTime time2 = DateTime.Now;
TimeSpan diff1 = time2 - time1;
label1.Text = diff1.Seconds.ToString();
}
private void button2_Click(object sender, System.EventArgs e)
{
int minValue;
int val1 = 5;
int val2 = 7;
DateTime time1 = DateTime.Now;
for(long i=0; i<1000000000; i++)
{
minValue = val1 < val2 ? val1 : val2;
}
DateTime time2 = DateTime.Now;
TimeSpan diff1 = time2 - time1;
label2.Text = diff1.Seconds.ToString();
}
This means that both the methods are compared by using the for loop for 10 billion iterations of the respective calculations. And the results are: the first method took 12 seconds whereas the second method took just 7 seconds.
So, you can safely use the second method.
|
|
|
|
|
Just curious, but what are the results if you randomly generated numbers instead of using the same two constant numbers over and over again?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|