Click here to Skip to main content
15,849,054 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a multiple threading WPF application, I want to update a slider value in a task.
C#
Dispatcher.BeginInvoke((Action)delegate()
{
   if (remainder.CallMethod == "Test")
       slider1.Value = slider1.Value - 1;
}

The problem is each thread can access the above code, I did not use lock
C#
lock(Object)
{
  Dispatcher.BeginInvoke((Action)delegate()
   {
     if (remainder.CallMethod == "Test")
       slider1.Value = slider1.Value - 1;
   }  
}

The result seems to be okay without lock, but do I have to use it?
Posted

1 solution

No, because when you do the Dispatcher.BeginInvoke you change thread to the UI thread. This means that while setting the slider value you are just running on one thread which would be the same as lock.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900