Click here to Skip to main content
15,919,613 members
Home / Discussions / C#
   

C#

 
AnswerRe: change form title (text) base on application Pin
Alexander Wiseman9-Jun-06 4:59
Alexander Wiseman9-Jun-06 4:59 
GeneralRe: change form title (text) base on application Pin
donkaiser9-Jun-06 5:20
donkaiser9-Jun-06 5:20 
QuestionDesign question Pin
Elina Blank9-Jun-06 4:52
sitebuilderElina Blank9-Jun-06 4:52 
AnswerRe: Design question Pin
LongRange.Shooter9-Jun-06 7:01
LongRange.Shooter9-Jun-06 7:01 
AnswerRe: Design question Pin
nevezuxa9-Jun-06 8:09
nevezuxa9-Jun-06 8:09 
AnswerRe: Design question Pin
BoneSoft9-Jun-06 11:32
BoneSoft9-Jun-06 11:32 
QuestionHow to remotely change system setting [modified] Pin
bannapradeep9-Jun-06 4:25
bannapradeep9-Jun-06 4:25 
QuestionLock on static object - Threading Question (long) [modified] Pin
Alexander Wiseman9-Jun-06 4:14
Alexander Wiseman9-Jun-06 4:14 
Hello,

Recently I have been looking into threading in .NET, specifically in C#. I think I understand the necessity of using the Monitor class (or the lock statement as a shortcut) to assure that reading and writing operations on shared data are thread-safe.

I am confused, however, about using the lock keyword on static and non-static objects, especially when the variable whose thread-safety I want to assure is in a class. This is a rather vague question, so here is some code to clarify:
class MyClass
{
     private readonly object locker;
     private String strToProtect;

     public String StringToProtect
     {
          get 
          {
               lock(locker) { return strToProtect; } 
          }
          set
          {
               lock(locker) { strToProtect = value; }
          }
     }

     private int anotherPrivateObject;

     public void ThreadFunction()
     {
          anotherPrivateObject = 0;   
          for(int i = 0; i < 10; i++)
          {
               anotherPrivateObject++;
               System.Threading.Thread.Sleep(100);
               System.Diagnostics.Trace(StringToProtect);
          }
     }
}
...
Main()
{
     MyClass instanceObject = new MyClass();
     instanceObject.StringToProtect = "set the string on this instance of MyClass";

     Thread myThread = new Thread(new ThreadStart(instanceObject.ThreadFunction));
     myThread.Start();

     instanceObject.StringToProtect = "changing this string should not be a problem";

     ...
}
Now my questions about this code are these:

1) Is this a proper way to lock an object inside a class and access it by multiple threads? (i.e. always going through StringToProtect's public accessor?)

2) Should locker be static? If so, why?

I think this question mainly arises from confusion about the static keyword - this confusion mainly arose from my recent investigation of threading, because many (if not all) of the examples I have seen use a static object to lock on.

Those two questions are the most important, but a third one occurs to me as well:

3) Is it wrong or bad-practice to have a thread function inside a class which is not a static function itself? Like, in my code example, the function MyClass.ThreadFunction. Is it a problem that this is not a static function and therefore can only be called when already possessing a created instance of MyClass?

Thank you very much in advance for your time. If I have not stated the questions clearly enough, or the code sample is confusing, please let me know, I will try to rephrase or rewrite that part of the question.

Sincerely,
Alexander Wiseman
AnswerRe: Lock on static object - Threading Question (long) Pin
Dustin Metzgar9-Jun-06 5:09
Dustin Metzgar9-Jun-06 5:09 
GeneralRe: Lock on static object - Threading Question (long) Pin
Alexander Wiseman9-Jun-06 5:14
Alexander Wiseman9-Jun-06 5:14 
AnswerRe: Lock on static object - Threading Question (long) Pin
LongRange.Shooter9-Jun-06 7:16
LongRange.Shooter9-Jun-06 7:16 
GeneralRe: Lock on static object - Threading Question (long) Pin
Alexander Wiseman9-Jun-06 7:37
Alexander Wiseman9-Jun-06 7:37 
Questionvalidate input Pin
donkaiser9-Jun-06 4:03
donkaiser9-Jun-06 4:03 
AnswerRe: validate input Pin
Dustin Metzgar9-Jun-06 4:24
Dustin Metzgar9-Jun-06 4:24 
GeneralRe: validate input Pin
donkaiser9-Jun-06 4:35
donkaiser9-Jun-06 4:35 
GeneralRe: validate input Pin
Dustin Metzgar9-Jun-06 4:51
Dustin Metzgar9-Jun-06 4:51 
AnswerRe: validate input Pin
Josh Smith9-Jun-06 4:24
Josh Smith9-Jun-06 4:24 
GeneralRe: validate input Pin
donkaiser9-Jun-06 4:40
donkaiser9-Jun-06 4:40 
GeneralRe: validate input Pin
Josh Smith9-Jun-06 5:15
Josh Smith9-Jun-06 5:15 
QuestionHow to run App from stream ? Pin
hdv2129-Jun-06 3:20
hdv2129-Jun-06 3:20 
AnswerRe: How to run App from stream ? Pin
Josh Smith9-Jun-06 4:00
Josh Smith9-Jun-06 4:00 
Questionsearch in combobox Pin
Mohammed Elkholy9-Jun-06 2:09
Mohammed Elkholy9-Jun-06 2:09 
AnswerRe: search in combobox Pin
Michael Potter9-Jun-06 6:16
Michael Potter9-Jun-06 6:16 
AnswerRe: search in combobox Pin
Drew McGhie9-Jun-06 8:36
Drew McGhie9-Jun-06 8:36 
GeneralRe: search in combobox Pin
Mohammed Elkholy11-Jun-06 8:21
Mohammed Elkholy11-Jun-06 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.