O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Use Synchronization Code with Multiple Threads

Problem

You need to access the same object from multiple threads, without causing a concurrency problem.

Solution

Use the SyncLock statement to gain exclusive access to the object.

Discussion

Trying to access an object on more than one thread at once is inherently dangerous, because a single command in a high-level language like Visual Basic .NET might actually compile to dozens of low-level machine language instructions. If two threads try to modify data at the same time, there’s a very real possibility that the changes made by one thread will be overwritten—and this is only one of many possible threading errors, all of which occur sporadically and are extremely difficult to diagnose.

To overcome these ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required