17 July 2012

A Cross-Thread Exception in DevExpress XPO

A cross-thread exception will happen every time you want to access a Session from different threads in the same time.

With the latest changes, DexExpress' Session introduced a Thread Watch object. His sole purpose is to check which thread tries to access a Session when it is about to chage it state from empty or to either GetObjectsNonReenterant or CommitTransactionNonReenterant state. If it is the different than the previous one, it will throw an InvalidOperationException exception.

Once the thread leaves the Session, Thread Watch will clear its state, so new thread can access it again.

Basically, it means that you have to use a single thread throughout the lifecycle of the Session object. You could, however, use more than one thread, as along as you use them sequentially, one after another. But, I believe that it would, if it isn't already naturall for your application, bring more troubles than benefits.

There is an option left to turn off this explicitly checks, by setting DevExpress.Xpo.Helpers.SessionStateStack.SuppressCrossThreadFailuresDetection to True, but it not advisable since it will just hide the real source of the problem.

No comments:

Post a Comment