|
Mark Churchill wrote: Declare it as volatile
I am not sure, but I think volatile doesn't make the type thread safe. It only allows to read the latest value written by other threads.
|
|
|
|
|
|
Access to all primitive types <=32 bit is atomic.
So reading and writing ints and bools is safe. But a "read-modify-write" operation is not atomic; so for integers, there's Interlocked.Increment etc.
Now, the only kind of "read-modify-write" I could image for bools is negating them. That's not safe.
But something like "if (!a) { a = true; }" is safe: if another thread happens to change the value between the "if (!a)" and the "a = true;", the only thing it can have done is setting a to true - but that's not going cause any problems, since we are about to set a to true anyway.
However, if you use the boolean for things like "run this code only once", then you have to lock/use Interlocked.CompareExchange to ensure that only the first thread to set the boolean to true runs the "run only once" code.
Remember that usually, there's no point in making all members of a class thread-safe, as combined operations need locking anyways, so the user of a class usually has to do his own locking.
E.g. given a thread-safe list, "if (!list.Contains(x)) list.Add(x);" is not thread-safe, so you need to "lock (list)" around every list access - but if you have to lock around every access, you could as well start with a non-thread-safe list.
|
|
|
|
|
Daniel Grunwald wrote: However, if you use the boolean for things like "run this code only once", then you have to lock/use Interlocked.CompareExchange to ensure that only the first thread to set the boolean to true runs the "run only once" code.
Remember that there is no Interlocked.CompareExchange for boolean types. You can use an INT and treat it like a boolean, but that's pretty inelegant.
Oddly enough, Microsoft has repeatedly chosen not to support booleans in the Interlocked methods, despite the many requests I found in their forums/Connection Feedbacks.
|
|
|
|
|
JoeRip wrote: You can use an INT and treat it like a boolean, but that's pretty inelegant.
That's what I do. If you need interlocked booleans frequently, create a custom bool type to make the calling code a bit more elegant:
public struct MyBoolean
{
public static readonly MyBoolean False = new MyBoolean(0);
public static readonly MyBoolean True = new MyBoolean(1);
int val;
private MyBoolean(int val) {
this.val = val;
}
public MyBoolean CompareExchange(MyBoolean value, MyBoolean comparand)
{
return new MyBoolean(Interlocked.CompareExchange(ref val, value.val, comparand.val));
}
public static implicit bool(MyBoolean a)
{
return a.val != 0;
}
public static implicit MyBoolean(bool a)
{
return a ? True : False;
}
public static bool operator ==(MyBoolean a, MyBoolean b)
{
return a.val == b.val;
}
}
|
|
|
|
|
Hello.
I have a question here.
Is it possible to create the following?
FTP Client(*) connects to FTP Server. Then using the connection that was established between them, the FTP Server now acts as FTP Client and the FTP Client as FTP Server, and can now access files on the (*).
If it's possible maybe i can try developing it...
Just trying to bypass router for the ingorant people.
|
|
|
|
|
Don't mind the (FTP).
I pretty good at sockets so i don't thing it would be that hard, if it can be done ofcourse.
|
|
|
|
|
Hello,
I am looking for a way to programatically edit the web.config file for the section below. Most importantly I will need to set the website login password to a certain value stored in the database. The password keeps changing in database whereas username remains the same. Can you please suggest a way this can be done in C# 2.0?
<authentication mode="Forms">
<forms loginurl="Login.aspx" timeout="60" protection="All">
<credentials passwordformat="Clear">
<user name="testuser" password="test123" />
</credentials>
</forms>
</authentication>
Thanks!
|
|
|
|
|
I reckon the folks in the ASP.NET forum may know the answer. I believe the answer is no, tho.
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|
|
I created a proxy application that sits locally, and a web service that communicates with my local proxy. I pass in a website and it passes back the raw data, pretty basic.
What I can't figure out how to do is get the images back as well for the page.
Take google for example, the page comes back fine, but the banner doesn't.
Should I parse the incoming HTML for the image tag then do a seperate request for that image? If so, how would I get it to the currently viewed page?
Any Idea's?
Hope this makes sense.
|
|
|
|
|
Anyone with suggestions??
|
|
|
|
|
Hi,
I have got a MDI app where one of the childforms must be able to see the status of a RadioButton on a docked panel on the MDI from. I want too use the mousedown event on a child frmShow to draw lines on the form if the radPoly was clicked on the pnlTools on the parent MDI form. Please help with a simple code example.
Thanks
Ian
|
|
|
|
|
Ian,
It isn't a real simple example, but you could take a look at my Extensions to DrawTools[^] article which has the type of ability (and more) you are looking for.
In that application, the MDI parent form sets properties on the DrawArea object to communicate settings from parent to child.
HTH
Sincerely,
-Mark
mark@msdcweb.com
http://www.msdcweb.com
|
|
|
|
|
Thanks Mark,
Nice app and very usefull info for something else that I want to do but no MDI stuff in there ?
cheers
Ian
|
|
|
|
|
Ian,
The DrawTools project uses a MDI parent form - I had forgotten it doesn't use any child forms.
Try this out for a solution to your problem:
In your child form expose some public or internal properties that are set when a toolbar button in the parent form is clicked. For example:
*** child form ***
public bool isPoly;
*** parent form ***
In the toolbarButton_Click(...) event:
if (ActiveMdiChild == null) return;
var child = ActiveMdiChild as childformclass;
child.isPoly = toolbarButton.Checked;
This way the child form(s) can see when a button on the toolbar in the parent is checked or not and react accordingly.
Additionally in the parent form you will want to react when the child form changes and set the state of buttons according to the active child form:
*** parent form ***
In MdiChildActivate event:
if (ActiveMdiChild == null) return;
var child = ActiveMdiChild as childformclass;
toolbarButton.Checked = child.isPoly;
HTH
Sincerely,
-Mark
mark@msdcweb.com
http://www.msdcweb.com
|
|
|
|
|
Thanks Mark,
This was really helpfull !! I used to work in VB6 Pro and thought about moving over to C#.Net instead of VB.Net. In order to learn the new C#.Net environment it seems you have to buy a lot of books (four up to date) and do the Microsoft certified courses and still all of it only shows you bits and pieces and end up becoming very expensive.
So what I am trying to say is that the code you supplied helps me a lot in grasping whats happening.
So thanks again for your patience.
Ian
|
|
|
|
|
Ian,
You are very welcome - glad I was able to help.
I came to C# after making a living programming in Visual FoxPro for close to 10 years. I picked up some Java experience along the way as well. When I started learning .NET and C# I too bought several books - the best one for me was Windows Forms 2.0 Programming[^].
Hang in there and don't be afraid to ask questions on CodeProject - everyone here is very helpful.
Sincerely,
-Mark
mamiller@rhsnet.org
|
|
|
|
|
Thanks Mark,
The book looks good and I will amazon it soon !!
Cheers
Ian
|
|
|
|
|
ianhunt01 wrote: I have got a MDI app where one of the childforms must be able to see the status of a RadioButton on a docked panel on the MDI from.
Why? Best Practice would be for views to get their data from the Model rather than another View. See Model-View-Controller[^]
led mike
|
|
|
|
|
No,
I had a look at the Wiki and it does not tell me much in a practical way of achieving this.
But thanks in any case
Ian
|
|
|
|
|
ianhunt01 wrote: No,
I had a look at the Wiki and it does not tell me much in a practical way of achieving this.
Ok, I guess unless you can copy/paste some code you can't do it. Good luck.
led mike
|
|
|
|
|
Anyone know of a SIMPLE TO USE syntax coloring control for use in a C# WinForms application?
Everything makes sense in someone's mind
|
|
|
|
|
|
Hello friends I have a function name " private void fnSave()". I want to call this function when ever I press " Ctrl+S " button. Just like in MS Word.Can any one help me plz?
|
|
|
|
|