I have a Dictionary of type (int, &lt; T >).  I have a helper method GetAll(); that gets me all the values as IEnumerable&lt; T >.  I sometimes get an exception When I try to use a lambda expression to search for a value, Like this:  myDictionary.GetAll().Where(x =&gt; x.Id == myID)   The search condition is valid and it exists.  The exception I get is:  System.InvalidOperationException: Collection was modified;  enumeration operation may not execute.    at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)    at System.Collections.Generic.Dictionary`2.ValueCollection.Enumerator.MoveNext()   at System.Linq.Enumerable.FirstOrDefault[TSource] (IEnumerable`1 source, Func`2 predicate)   I checked and found that using ToList() might resolve my problem. I tried,  but the issue reoccurred. Can anyone help?          EDIT - Dictionary is being called/shared by multiple threads/functions/classes simultaneously. Putting in a lock would be painful and have performance overheads. The issue is rare and random to reproduce.          EDIT2 : Used ToList(),  doesn't help.     Here's the GetAll method :     It does use the Values internally, just has a lock object wrapped over it.      public IEnumerable&lt;U&gt; GetAll()     {         IEnumerable&lt;U&gt; values;         lock (lockObj)         {             values = myDictionary.Values;         }         return values;     }
I can't get to the bottom of this error, because when the debugger is attached, it does not seem to occur.  Collection was modified; enumeration operation may not execute  Below is the code. This is a WCF server in a Windows service. The method NotifySubscribers() is called by the service whenever there is a data event (at random intervals, but not very often - about 800 times per day). When a Windows Forms client subscribes, the subscriber ID is added to the subscribers dictionary, and when the client unsubscribes, it is deleted from the dictionary. The error happens when (or after) a client unsubscribes. It appears that the next time the NotifySubscribers() method is called, the foreach() loop fails with the error in the subject line. The method writes the error into the application log as shown in the code below. When a debugger is attached and a client unsubscribes, the code executes fine. Do you see a problem with this code? Do I need to make the dictionary thread-safe? [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)] public class SubscriptionServer : ISubscriptionServer {     private static IDictionary&lt;Guid, Subscriber&gt; subscribers;      public SubscriptionServer()     {                     subscribers = new Dictionary&lt;Guid, Subscriber&gt;();     }      public void NotifySubscribers(DataRecord sr)     {         foreach(Subscriber s in subscribers.Values)         {             try             {                 s.Callback.SignalData(sr);             }             catch (Exception e)             {                 DCS.WriteToApplicationLog(e.Message,                    System.Diagnostics.EventLogEntryType.Error);                  UnsubscribeEvent(s.ClientId);             }         }     }          public Guid SubscribeEvent(string clientDescription)     {         Subscriber subscriber = new Subscriber();         subscriber.Callback = OperationContext.Current.                 GetCallbackChannel&lt;IDCSCallback&gt;();          subscribers.Add(subscriber.ClientId, subscriber);                  return subscriber.ClientId;     }      public void UnsubscribeEvent(Guid clientId)     {         try         {             subscribers.Remove(clientId);         }         catch(Exception e)         {             System.Diagnostics.Debug.WriteLine(&quot;Unsubscribe Error &quot; +                      e.Message);         }     } }
How to keep the spaces at the end and/or at the beginning of a String? I need to build a list of files present in a directory but also pass in a file containing a list of directories to exclude.  I've seen the below suggestion but I've had no luck with it when it comes to the type flag:  find $dir_name $(printf "! -name %s " $(cat $exclude_file))   Is there a way I can do it or will I need to create the entire list and then work through it removing ones that partially match ones in the exclude directory. Scenario:   Create SymLink: C:\Data\workspaces --&gt; C:\Users\****\Documents\workspaces Navigate to C:\Data\Workspaces in explorer Click and drag the the path from the address bar onto "Quick Access" in the navigation pane.   Expected Result:   Clicking on the shortcut takes you to C:\Data\workspaces   Actual Result:   Clicking on the shortcut takes you to C:\Users\****\Documents\workspaces   How do you make it so that the shortcut points to the correct path. (the SymLink)  Command Used to create the SymLink:  mklink /D C:\Data\workspaces C:\Users\****\Documents\workspaces   Updated with pictures:  Please note the workspaces symlink inside C:\Data:    When I double-click on the symlink it works correctly. (note the address bar.    When i pin it to the Quick access bar, however, it uses the address that the symlink points to and NOT the symlink address. Throwing errors on a standard page layout using Apex! Scenario: I have a custom button on a standard layout, which will call an apex class to do some validation on a record. If not validated, it needs to throw out some errors on that page. What are the best UX options to achieve this?  I am not too sure how to achieve this. Within a trigger, you can use the addError method to add the custom errors.  Can my Apex class reference the fields of a standard object? The ideal approach would be to do the following:  Contact.Email.addError('Please enter the right format here');   How can I achieve this via an apex class that the custom button will call?  What I have thought: Having a VF Page popup on the screen so that user can see all the errors, but that's not the best approach as UX is a big deal for this application  Thanks. How to download all required Ubuntu drivers Is there a package of Ubuntu drivers as driver pack solution? I tried to search for it, but I did not find it. If there is, how can I easily get it? Why do shell script comparisons often use x$VAR = xyes? We can prove that there is no integer $n&gt;1$ such that $2^n-1\mid 3^n-1$. This leads to the following question:     Is it true that for every pair of primes $p&lt;q$ there are only finitely many integers $n$ such that $p^n-1\mid q^n-1$? Are there two primes $p&lt;q$ and an integer $n&gt;p+q$ such that $p^n-1\mid q^n-1$?Is it true that if $n&gt;6$ then $2^n-1\nmid 5^n-1$?   Edit: Here are some examples: \begin{array}{ll} 2^{36}-1\mid 41^{36}-1, &amp;3^{12}-1\mid 97^{12}-1,\\ 5^{6}-1\mid 37^{6}-1, &amp;7^{4}-1\mid 151^{4}-1. \end{array}  Now I prove there is no integer $n&gt;1$ such that $2^n-1\mid 3^n-1.$  Proof: Denote $A=2^n-1$ and $B=3^n-1$. If $n$ is even then $3$ divides $A$ but not $B$, a contradiction.  If $n$ is odd then $A\equiv -5\pmod {12}$. Since every prime greater than $3$ is $\equiv \pm1,\pm5 \pmod {12}$, some prime factor $p$ of $A$ must be congruent to $\pm5 \pmod {12}$. As $p \mid B$, we have $3^{n+1}\equiv 3 \pmod p$. Since $n+1$ is even, we get $(\frac{3}{p})=1$, a contradiction again. Are logs kept for past at jobs? Occasionally I see constructs like this in English sentences:        Not only is this true in this case, but ...      What puzzles me is the "is this" part of it. The sentence is not a question, yet it seems to have a question-like order. Isn't this correct/preferred:        Not only this is true in this case, but ...      Are there other cases where non-question sentences contain question-like parts (so to speak)? Sequence uniform convergence but the derivatives are not. Give a sequence $(f_n)_{n\in \mathbb{N}}$ of differentiable functions which uniformly converge to $0$, but for which the seqeunce $(f_n')_{n\in \mathbb{N}}$ of the derivatives isn't even pointwise convergent.    I found this one in my textbook marked as "Fun things to solve" and although it may be fun, it's kind of hard to do.   To be honest I already failed at the first hurdle. I couldn't even find a sequence of functions which uniformly converges to $0$.   Is there a certain way of dealing with this kind of problem? Because it seems kind of hard for me to come up with sequences without a mathematical of way of doing so (or maybe there is a mathematical way I just don't know yet). How to get the CPU cycle count in x86_64 from C++? Can I switch the User-Agent header for only a single web site? Forecast total for a year given monthly time series I have a monthly time series (for 2009-2012 non-stationary, with seasonality). I can use ARIMA (or ETS) to obtain point and interval forecasts for each month of 2013, but I am interested in forecasting the total for the whole year, including prediction intervals. Is there an easy way in R to obtain interval forecasts for the total for 2013? mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource resizable serial console window? When using the serial console of my system I always end up with $COLUMNS=80 and $LINES=24.  While I can change these variables manually it is somewhat annoying to do this any time when the client side terminal window has been resized.  Usually I'm connecting to the console using screen /dev/mytty baudrate.  Changing the $TERM environment variable to "screen" or "xterm" does not help.  Will I need to call getty with some of those instead of vt100?  Needless to say that all of this works fine, when I connect to the same machine using ssh. Re-Structuring a JSON Shell scripting: Way to parse parameters from command line myscript [-a a-arg] [-c c-arg] [-b] [-e] somedirectory   Given that I want my shell script be invoked at the command line using the above parameters - where [these brackets] denote that they are optional - what is the best method to parse them? What is the command to open the shutdown dialog? I would like to use a gesture to shutdown/restart my computer and I need to know the command that launches the shutdown dialog: Why is $1^{\infty}$ considered to be an indeterminate form Proof of the square root inequality $2\sqrt{n+1}-2\sqrt{n}<\frac{1}{\sqrt{n}}<2\sqrt{n}-2\sqrt{n-1}$