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

C#

 
GeneralRe: dropdownlist in web control Pin
N a v a n e e t h11-Oct-07 21:21
N a v a n e e t h11-Oct-07 21:21 
GeneralRe: dropdownlist in web control Pin
Sonia Gupta11-Oct-07 22:17
Sonia Gupta11-Oct-07 22:17 
Questionchild node in tree view Pin
chanzeb11-Oct-07 18:41
chanzeb11-Oct-07 18:41 
AnswerRe: child node in tree view Pin
Rocky#11-Oct-07 19:50
Rocky#11-Oct-07 19:50 
Questionpassing arguments Pin
rameshdontagani11-Oct-07 18:38
rameshdontagani11-Oct-07 18:38 
AnswerRe: passing arguments Pin
Martin#11-Oct-07 21:06
Martin#11-Oct-07 21:06 
QuestionApplication running in thread environment Pin
MP9111-Oct-07 16:21
MP9111-Oct-07 16:21 
AnswerRe: Application running in thread environment Pin
Scott Dorman11-Oct-07 16:32
professionalScott Dorman11-Oct-07 16:32 
If you're running this code in a threaded environment you are running into problems because it's not thread safe. The Debug.Assert call itself isn't threadsafe and neither are the DecrementStock and IncrementStock methods. The most likely explanation is that you have multiple threads manipulating the stockCount variable at the same time which is giving unpredictable behavior.

There are a lot of different ways to protect against this, from using the lock keyword to using InterlockExchangeCompare. The easiest is probably the lock syntax, which would look something like this:
C#
public class Warehouse
{
   private object syncRoot = new object();
   private int stockCount = 0;
 
   public void DecrementStock ()
   {
      lock(syncRoot)
      {
         if(stockCount > 0)
         {
            stockCount--;
         }
         Debug.Assert(stockCount >= 0);
      }
   }
 
   public void IncrementStock()
   {
      lock(syncRoot)
      {
         stockCount++;
      }
   }
}
Also, in the future, please use the <pre> tags for code blocks to preserve the formatting.


Scott.

—In just two days, tomorrow will be yesterday.

[Forum Guidelines] [Articles] [Blog]

QuestionBackup Database using C# Pin
C#Coudou11-Oct-07 15:13
C#Coudou11-Oct-07 15:13 
AnswerRe: Backup Database using C# Pin
Charith Jayasundara11-Oct-07 18:34
Charith Jayasundara11-Oct-07 18:34 
AnswerRe: Backup Database using C# Pin
Rocky#11-Oct-07 20:03
Rocky#11-Oct-07 20:03 
QuestionZooming a group of controls Pin
wolfshad311-Oct-07 14:33
wolfshad311-Oct-07 14:33 
AnswerRe: Zooming a group of controls Pin
Paul Conrad12-Oct-07 13:33
professionalPaul Conrad12-Oct-07 13:33 
GeneralRe: Zooming a group of controls Pin
wolfshad312-Oct-07 13:39
wolfshad312-Oct-07 13:39 
QuestionAccess to Object (TextBox or ListBox) from a Thread Pin
Patricio Tapia11-Oct-07 12:56
Patricio Tapia11-Oct-07 12:56 
AnswerRe: Access to Object (TextBox or ListBox) from a Thread Pin
Luc Pattyn11-Oct-07 13:25
sitebuilderLuc Pattyn11-Oct-07 13:25 
QuestionRe: Access to Object (TextBox or ListBox) from a Thread Pin
Patricio Tapia11-Oct-07 15:34
Patricio Tapia11-Oct-07 15:34 
AnswerRe: Access to Object (TextBox or ListBox) from a Thread Pin
Luc Pattyn11-Oct-07 15:50
sitebuilderLuc Pattyn11-Oct-07 15:50 
GeneralRe: Access to Object (TextBox or ListBox) from a Thread Pin
Patricio Tapia11-Oct-07 19:41
Patricio Tapia11-Oct-07 19:41 
Questionc# calling unmanaged C Pin
j10000_18611-Oct-07 12:53
j10000_18611-Oct-07 12:53 
GeneralRe: c# calling unmanaged C [modified] Pin
j10000_18611-Oct-07 13:03
j10000_18611-Oct-07 13:03 
GeneralRe: c# calling unmanaged C Pin
Luc Pattyn11-Oct-07 13:27
sitebuilderLuc Pattyn11-Oct-07 13:27 
GeneralRe: c# calling unmanaged C Pin
j10000_18611-Oct-07 13:41
j10000_18611-Oct-07 13:41 
GeneralRe: c# calling unmanaged C Pin
Luc Pattyn11-Oct-07 14:03
sitebuilderLuc Pattyn11-Oct-07 14:03 
GeneralRe: c# calling unmanaged C Pin
j10000_18611-Oct-07 14:52
j10000_18611-Oct-07 14:52 

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.