Click here to Skip to main content
15,908,274 members
Home / Discussions / C#
   

C#

 
AnswerRe: Is anything wrong with this? Pin
Heath Stewart11-Feb-04 12:08
protectorHeath Stewart11-Feb-04 12:08 
The thread proc is the method that's being executed in a separate thread, so no, the local variable i will NOT be affected. It's only when all the threads access a shared resource - like if you declared i as a field in the class that contains the thread proc. In this case, you can simply use the lock keyword against a shared object (typically in this case, you can lock against typeof(MyClass) so that only one thread can access that object at a time:
public class MyClass
{
  private int i;
  public MyClass()
  {
    for (int j=0; j<5; j++)
      new Thread(new ThreadStart(this.Increment)).Start();
  }
  private void Increment()
  {
    lock(this) // or typeof(MyClass), but this uses only one instance of MyClass
      i++;
  }
}


 

Microsoft MVP, Visual C#
My Articles
GeneralDatabinding to combobox Pin
Anonymous11-Feb-04 10:34
Anonymous11-Feb-04 10:34 
GeneralRe: Databinding to combobox Pin
Heath Stewart11-Feb-04 12:00
protectorHeath Stewart11-Feb-04 12:00 
Generalmanaging Deletes and Backspaces Pin
obelisk2911-Feb-04 10:19
obelisk2911-Feb-04 10:19 
GeneralRe: managing Deletes and Backspaces Pin
Heath Stewart11-Feb-04 11:57
protectorHeath Stewart11-Feb-04 11:57 
Generalmanaged c++ class that inherits from unmanaged c++ Pin
godzooky11-Feb-04 8:30
godzooky11-Feb-04 8:30 
GeneralRe: managed c++ class that inherits from unmanaged c++ Pin
Heath Stewart11-Feb-04 9:47
protectorHeath Stewart11-Feb-04 9:47 
GeneralRe: managed c++ class that inherits from unmanaged c++ Pin
godzooky11-Feb-04 9:50
godzooky11-Feb-04 9:50 
GeneralRe: managed c++ class that inherits from unmanaged c++ Pin
Heath Stewart11-Feb-04 9:52
protectorHeath Stewart11-Feb-04 9:52 
GeneralRe: managed c++ class that inherits from unmanaged c++ Pin
godzooky11-Feb-04 10:07
godzooky11-Feb-04 10:07 
GeneralRe: managed c++ class that inherits from unmanaged c++ Pin
Heath Stewart11-Feb-04 10:10
protectorHeath Stewart11-Feb-04 10:10 
GeneralMouse Button &gt; keystroke Pin
Spanky311-Feb-04 8:04
Spanky311-Feb-04 8:04 
GeneralRe: Mouse Button &gt; keystroke Pin
Kentamanos11-Feb-04 8:15
Kentamanos11-Feb-04 8:15 
GeneralRe: Mouse Button &gt; keystroke Pin
Daniel Turini11-Feb-04 8:16
Daniel Turini11-Feb-04 8:16 
GeneralRe: Mouse Button &gt; keystroke Pin
Spanky311-Feb-04 8:28
Spanky311-Feb-04 8:28 
GeneralRe: Mouse Button &gt; keystroke Pin
Kentamanos11-Feb-04 9:20
Kentamanos11-Feb-04 9:20 
GeneralRe: Mouse Button &gt; keystroke Pin
Anonymous11-Feb-04 9:31
Anonymous11-Feb-04 9:31 
GeneralRe: Mouse Button > keystroke Pin
Kentamanos11-Feb-04 10:36
Kentamanos11-Feb-04 10:36 
GeneralRe: Mouse Button &gt; keystroke Pin
Anonymous11-Feb-04 10:59
Anonymous11-Feb-04 10:59 
GeneralRe: Mouse Button &gt; keystroke Pin
Spanky311-Feb-04 11:16
Spanky311-Feb-04 11:16 
GeneralRe: Mouse Button > keystroke Pin
Kentamanos11-Feb-04 11:30
Kentamanos11-Feb-04 11:30 
GeneralRe: Mouse Button &gt; keystroke Pin
Spanky311-Feb-04 11:44
Spanky311-Feb-04 11:44 
GeneralRe: Mouse Button &gt; keystroke Pin
Kentamanos11-Feb-04 11:55
Kentamanos11-Feb-04 11:55 
GeneralRe: Mouse Button &gt; keystroke Pin
Spanky311-Feb-04 11:54
Spanky311-Feb-04 11:54 
GeneralNearly Sorted! Pin
Spanky311-Feb-04 12:02
Spanky311-Feb-04 12:02 

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.