Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 114109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:48:30+00:00 2026-05-11T02:48:30+00:00

Today I had to fix some older VB.NET 1.0 code which is using threads.

  • 0

Today I had to fix some older VB.NET 1.0 code which is using threads. The problem was with updating UI elements from the worker thread instead of the UI-thread. It took me some time to find out that I can use assertions with InvokeRequired to find the problem.

Besides the above mentioned concurrent modification problem, there are deadlocks, race conditions, etc. one could run into. As debugging/fixing threading problems is a pain, I’m wondering how I could reduce coding errors/faults in this area and how I could easier find any of them. So, what I’m asking for, is:

  • Are there any good patterns to follow when writing multi-threading code? What are the Dos and Don’ts?
  • What techniques do you use to debug threading problems?

Please provide some example code if applicable and possible. The answers should be related to the .NET framework (any version).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:48:30+00:00Added an answer on May 11, 2026 at 2:48 am

    This could be a massive list – read Joe Duffy’s excellent ‘Concurrent Programming On Windows‘ for much more detail. This is pretty much a brain dump…

    • Try to avoid calling into significant chunks of code while you own a lock
    • Avoid locking on references which code outside the class might also lock on
    • If you ever need to acquire more than one lock at a time, always acquire those locks in the same order
    • Where reasonable, use immutable types – they can be shared freely between threads
    • Other than immutable types, try to avoid the need to share data between threads
    • Avoid trying to make your types threadsafe; most types don’t need to be, and usually the code which needs to share data will need to control the locking itself
    • In a WinForms app:
      • Don’t perform any long-running or blocking operations on the UI thread
      • Don’t touch the UI from any thread other than the UI thread. (Use BackgroundWorker, Control.Invoke/BeginInvoke)
    • Avoid thread-local variables (aka thread-statics) where possible – they can lead to unexpected behaviour, particularly on ASP.NET where a request may be served by different threads (search for ‘thread agility’ and ASP.NET)
    • Don’t try to be clever. Lock-free concurrent code is hugely difficult to get right.
    • Document the threading model (and thread safety) of your types
    • Monitor.Wait should almost always be used in conjunction with some sort of check, in a while loop (i.e. while (I can’t proceed) Monitor.Wait(monitor))
    • Consider the difference between Monitor.Pulse and Monitor.PulseAll carefully every time you use one of them.
    • Inserting Thread.Sleep to make a problem go away is never a real fix.
    • Have a look at ‘Parallel Extensions’ and the ‘Coordination and Concurrency Runtime’ as ways of making concurrency simpler. Parallel Extensions is going to be part of .NET 4.0.

    In terms of debugging, I don’t have very much advice. Using Thread.Sleep to boost the chances of seeing race conditions and deadlocks can work, but you’ve got to have quite a reasonable understanding of what’s wrong before you know where to put it. Logging is very handy, but don’t forget that the code goes into a sort of quantum state – observing it via logging is almost bound to change its behaviour!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 216k
  • Answers 216k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That's a good idea when you want the user to… May 12, 2026 at 11:04 pm
  • Editorial Team
    Editorial Team added an answer Please see: RIA Services Overview - 4.8.1 Returning Related Entities.… May 12, 2026 at 11:04 pm
  • Editorial Team
    Editorial Team added an answer You can listen to the SelectionChanged event in the code-behind… May 12, 2026 at 11:04 pm

Related Questions

I am having a hard time figuring out why when the cfquery runs in
Possibly similar question: Do you ever use the volatile keyword in Java? Today I
I am using Visual Studio 2008, and I had an ASP.NET dev configuration working
Today I had to reinstall. I used to have some hotfixes installed for VS2008

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.