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 7403989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:11:28+00:00 2026-05-29T05:11:28+00:00

Assume following scenario: There is a WPF window, within a MVVM implementation. Having following

  • 0

Assume following scenario:
There is a WPF window, within a MVVM implementation. Having following code in it’s code behind file (acutally I know I rather should use ViewModel first approach, but using View First is enough at this stage):

public MainWindow()
{
    InitializeComponent();
    this.DataContext = MainWindowViewModel.GetInstance();
}

within the constructor of

ClassStartingWorkerThread instance;
MainWindowViewModel()
{
    instance = new ClassStartingWorkerThread();
}

I initialize an instance of a class which starts a Thread on construction like shown below:

ClassStartingWorkerThread()
{
    StartThread();
}

private Thread mEventRequestingThread = null;
private void StartThread()
{
    ThreadStart cDelegate = new ThreadStart(EventListening);
    mEventRequestingThread = new Thread(cDelegate);
    mEventRequestingThread.Start();
}

This class implements the IDisposable interface.
Therefore I call my Dispose method manually on Destruction like this:

~ClassStartingWorkerThread()
{
    Dispose(false);
}

public void Dispose()
{
    mEventRequestingThread.Abort();
}

At least you shall know: I start my application from Visual Studio.
Now, I’m closing my UI Window and would expect my routine to destruct created objects.
But the the Visual Studio does not go back into “edit” mode and stays in “debug” mode.

I guess there’s something wrong with my thread routine, but I’m wondering why neither the MainWindow destructor nor the MainWindowViewModel destructor is called.

This leads to the question on how I can manually start the destruction routine and tell my worker thread, which actually seems to block this, to stop?

Thanks in advance,
Thomas

  • 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. Editorial Team
    Editorial Team
    2026-05-29T05:11:28+00:00Added an answer on May 29, 2026 at 5:11 am

    The problem is that your destructor ~ClassStartingWorkerThread() is never called by the garbage collector. The instance of that class cannot be garbage-collected since the executing thread references it via the ThreadStart delegate. Generally you should never rely on a destructor beeing called at all. See the MSDN on C# Destructors.

    In your case simply set the Thread.IsBackground property to true. This will make the thread terminate automatically when the application shuts down.

    If you really have to have manual control over the termination of your worker thread, you shouldn’t abort it, but instead let it wait for (or cyclically check) some WaitHandle and terminate itself when the WaitHandle is signalled. When it comes to termination, Set the WaitHandle in your main thread, perhaps in a Window.Closing event handler.

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

Sidebar

Related Questions

I'm asking myself how to apply MVVM in the following scenario correctly: Let's assume
Assume the following scenario (with relational databases), class Currency { long id; String code;
Assume the following scenario. I have two files A.cpp and B.cpp in trunk. At
Assume the following code: using (SqlConnection conn = new SqlConnection(connectionString)) { ... using (SqlCommand
Assume that the following Perl code is given: my $user_supplied_string = &retrieved_from_untrusted_user(); $user_supplied_string =~
Trying to figure out how to best handle the following scenario: Assume a RequestContext
Let's assume the following scenario: Person A: calls person B. Person B with an
Assume the following scenario. i call a method like this String[] arr = {1,2,3};
Consider the following code: Class Demo { Person person = new Person(foo); // assume
Let's assume we have the following generic scenario: An RDBMS as a data source,

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.