Click here to Skip to main content
15,898,222 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# coding problem Pin
Jemami15-Sep-09 15:00
Jemami15-Sep-09 15:00 
GeneralRe: C# coding problem Pin
Jemami15-Sep-09 15:03
Jemami15-Sep-09 15:03 
GeneralRe: C# coding problem Pin
Luc Pattyn15-Sep-09 15:37
sitebuilderLuc Pattyn15-Sep-09 15:37 
QuestionThreads an Objects Pin
FJJCENTU15-Sep-09 12:27
FJJCENTU15-Sep-09 12:27 
AnswerRe: Threads an Objects Pin
Christian Graus15-Sep-09 12:36
protectorChristian Graus15-Sep-09 12:36 
GeneralRe: Threads an Objects Pin
FJJCENTU15-Sep-09 12:55
FJJCENTU15-Sep-09 12:55 
GeneralRe: Threads an Objects Pin
Christian Graus15-Sep-09 14:02
protectorChristian Graus15-Sep-09 14:02 
AnswerRe: Threads an Objects Pin
N a v a n e e t h15-Sep-09 16:48
N a v a n e e t h15-Sep-09 16:48 
FJJCENTU wrote:
I see in near every example using threads that the method to execute in the starting of the threads belong to a different class


Its all personal preferences. I never seen such a code so far though.

1) Not sure what you meant.
2) No. It can use. Make sure you are using proper synchronization mechanisms.
3) Declare it in such a way that it is accessible to both threads. See the following code,
class Foo
{
   List<string> strings = new List<string>();

   void StartThreads()
   {
      Thread t1 = new Thread(Thread1Execute);
      Thread t2 = new Thread(Thread2Execute);
      t1.Start();
      t2.Start();
   }  

   void Thread1Execute()
   {
      // this can access strings variable
   }

   void Thread2Execute()
   {
      // this can access strings variable
   }
}
If your code is doing any modifications to the strings collection, you should use some synchronization mechanisms.

FJJCENTU wrote:
in such case I don`t understand to use Lock(this) or Monitor(this), I`m not sinchronizing all the class but only a method.


Using lock(this) is a bad idea. You can choose any object as your locking object. When choosing a locking object, consider the following points.

  1. The object should not be modifiable without your knowledge. See the following code
    class Foo
    {
        public object locker = new object();
    }
    In this any one can modify the locker object from outside of class Foo without your knowledge. This is dangerous.
  2. All the threads that require this object should be able to access the locker object.
Above class can be written like the below with lock.
class Foo
{
   List<string> strings = new List<string>();
   readonly object locker = new object();

   void StartThreads()
   {
      Thread t1 = new Thread(Thread1Execute);
      Thread t2 = new Thread(Thread2Execute);
      t1.Start();
      t2.Start();
   }  

   void Thread1Execute()
   {
       lock(locker)
       {
           // your code
       }
   }

   void Thread2Execute()
   {
       lock(locker)
       {
           // your code
       }
   }
}
Monitor(this) won't compile. lock is a syntactic sugar which will be compiled as Monitor.Enter and Monitor.Exit.

Smile | :)


AnswerRe: Threads an Objects Pin
carlecomm21-Sep-09 21:42
carlecomm21-Sep-09 21:42 
QuestionCombo Boxes and Database Binding Pin
mbangh15-Sep-09 12:08
mbangh15-Sep-09 12:08 
AnswerRe: Combo Boxes and Database Binding Pin
Christian Graus15-Sep-09 12:37
protectorChristian Graus15-Sep-09 12:37 
GeneralRe: Combo Boxes and Database Binding Pin
mbangh15-Sep-09 12:43
mbangh15-Sep-09 12:43 
AnswerRe: Combo Boxes and Database Binding Pin
Abhishek Sur15-Sep-09 13:00
professionalAbhishek Sur15-Sep-09 13:00 
AnswerRe: Combo Boxes and Database Binding Pin
N a v a n e e t h15-Sep-09 17:33
N a v a n e e t h15-Sep-09 17:33 
GeneralRe: Combo Boxes and Database Binding Pin
mbangh16-Sep-09 5:56
mbangh16-Sep-09 5:56 
AnswerRe: Combo Boxes and Database Binding Pin
carlecomm21-Sep-09 21:30
carlecomm21-Sep-09 21:30 
QuestionXML Data and Data Sets Pin
AndyASPVB15-Sep-09 10:19
AndyASPVB15-Sep-09 10:19 
AnswerRe: XML Data and Data Sets Pin
Ian Shlasko15-Sep-09 10:54
Ian Shlasko15-Sep-09 10:54 
GeneralRe: XML Data and Data Sets Pin
AndyASPVB15-Sep-09 11:39
AndyASPVB15-Sep-09 11:39 
GeneralRe: XML Data and Data Sets Pin
Ian Shlasko15-Sep-09 12:05
Ian Shlasko15-Sep-09 12:05 
AnswerRe: XML Data and Data Sets Pin
carlecomm21-Sep-09 21:28
carlecomm21-Sep-09 21:28 
QuestionSource Code Encryption Pin
kKamel15-Sep-09 10:03
kKamel15-Sep-09 10:03 
AnswerRe: Source Code Encryption Pin
Manas Bhardwaj15-Sep-09 10:07
professionalManas Bhardwaj15-Sep-09 10:07 
GeneralRe: Source Code Encryption Pin
kKamel15-Sep-09 10:09
kKamel15-Sep-09 10:09 
GeneralRe: Source Code Encryption Pin
Christian Graus15-Sep-09 10:15
protectorChristian Graus15-Sep-09 10:15 

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.