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

  • SEARCH
  • Home
  • 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 372539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:13:14+00:00 2026-05-12T14:13:14+00:00

I am currently investigating how Thread.Interrupt plays together with P/Invoke or native calls. I

  • 0

I am currently investigating how Thread.Interrupt plays together with P/Invoke or native calls. I have read in MSDN that it is not possible to abort (Thread.Abort) a thread which is in the native call (other use cases might apply as well). But I did not find any reference which states the same for native threads which are in WaitSleepJoin state.

This question is not about whether one should call Abort or Interrupt, but about where I can find authorative doc on this. G-ing for this did not provide any useful output.

My test example:

#ifdef NATIVEFUNCTIONS_EXPORTS
#define NATIVEFUNCTIONS_API __declspec(dllexport)
#else
#define NATIVEFUNCTIONS_API __declspec(dllimport)
#endif

#include <iostream>

extern "C"
{
  NATIVEFUNCTIONS_API void EndlessWait(char const* mutexName)
  {
    std::cout << "entering the endless wait." << std::endl;

    HANDLE mutex = CreateMutex(NULL, FALSE, mutexName);
    WaitForSingleObject(mutex, INFINITE);

    std::cout << "leaving the endless wait." << std::endl;
  }

};

Native C++-DLL which exports a function, that endlessly waits on a mutex.

Now the C# .NET counterpart, which tries to cancel the wait:

using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace InterruptingNativeWaitingThread
{
  class Program
  {
    [DllImport("NativeFunctions.dll", CharSet=CharSet.Ansi)]
    static extern void EndlessWait(string str);

    static void Main(string[] args)
    {
      string mutexName = "interprocess_mutex";
      Mutex m = new Mutex(false, mutexName);
      m.WaitOne();
      Thread t = new Thread(() => { EndlessWait(mutexName); });
      t.Start();
      Thread.Sleep(1000);

      t.Abort();
      if(!t.Join(5000))
        Console.WriteLine("Unable to terminate native thread.");

      t.Interrupt();
      if(!t.Join(5000))
        Console.WriteLine("Unable to interrupt the native wait.");

      Console.WriteLine("Release the mutex.");
      m.ReleaseMutex();
      t.Join();
    }
  }
}

Executing this app produces the following output:

entering the endless wait.
Unable to terminate native thread.
Unable to interrupt the native wait.
Release the mutex.
leaving the endless wait.

Abort does not work in this context as expected, but msdn does not say a word about interrupt. I would expect it work on one hand: since the managed thread being in Wait state also calls the native WaitForSingleObject or WaitForMultipleObjects; on the other hand, there is a possibility that native thread to be interrupted does not support expect exceptions all, what than?

Any doc is very welcome!

Many thanks,
Ovanes

P.S. I also found in MSDN that abort waits until the thread to be aborted returns from the unmanaged code and first call interrupt if the thread is in WaitSleepJoin state and than aborts it. But that does not mean that interrupt can’t interrupt native WaitSleepJoin.

  • 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-12T14:13:14+00:00Added an answer on May 12, 2026 at 2:13 pm

    I doubt that the thread is in the WaitSleepJoin state; Interrupt is documented to only interrupt threads in this state. Look at the ThreadState property of the thread to verify what state it is in.

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

Sidebar

Related Questions

I have a .NET/native C++ application. Currently, the C++ code allocates memory on the
I am currently investigating a performance issue in an application and have highlighted the
I am currently investigating several free/open source OpenGL based 3D engines, and was wondering
I am currently investigating how to make a connection to a SQL Server database
We are currently investigating different remote-desktop support solutions to help our clients if they
I come from a CVS background. I'm currently investigating using SVN for a project.
Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and
Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database.
Currently we have a project with a standard subversion repository layout of: ./trunk ./branches
I'm currently investigating how to use the RMI distribution option in ehcache. I've configured

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.