ManagedThreadID和操作系统的ThreadID的关系 [英] Relationship between ManagedThreadID and Operating System ThreadID

查看:2008
本文介绍了ManagedThreadID和操作系统的ThreadID的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个多线程的C#Windows应用程序的频繁调用到本机DLL。这些被阻塞调用它有时会持续相当长的时间。

在某些情况下,我想从主线程我正在使用的原生API提供了一个功能,为此取消了一些工作线程这些阻塞调用:

  HRESULT CancelBlockingCall(DWORD的ThreadID)
 

虽然对于CancelBlockingCall()的文件是不是非常清楚,我认为我需要传递的ThreadID的操作系统级的线程被阻塞的通话。根据返回codeS,我从CancelBlockingCall()得到,我意识到Thread.ManagedThreadID是不是我所需要的。我发现 MSDN以下(见注)

  

这是操作系统的ThreadId没有固定的关系提高到一个托管线程,因为   非托管主机可以控制托管和非托管线程之间的关系。   具体而言,一个成熟的主机可以使用CLR托管API安排许多管理   针对同一操作系统线程的线程,或者移动至之间托管线程   不同的操作系统线程。

这是否意味着,有没有办法对我来说,正确地调用CancelBlockingCall()的托管线程?是无法确定的操作系统级线程的的ThreadId其中托管线程正在执行?

解决方案
  

这是否意味着,有没有办法对我来说,正确地调用CancelBlockingCall()的托管线程?是无法确定的操作系统级线程的的ThreadId其中托管线程正在执行?

由于艾丹说,你可以使用GetCurrentThreadID API来获取操作系统的线程ID。

要跟踪它在整个管理线程,你可以在你存储操作系统线程ID一类包装您的API调用,这样就可以在以后停止它:

 公共类APITask
{
    私人UINT _osThreadId;

    公共无效的run()
    {
        _osThreadId = GetCurrentThreadID();
        API.RunBlockingMethod();
    }

    公共无效取消()
    {
        API.CancelBlockingCall(_osThreadId);
    }
}
 

I'm working on a multi-threaded C# Windows application that makes frequent calls into a native dll. These are blocking calls which can sometimes last quite a long time.

In certain situations, I'd like to cancel these blocking calls on some worker threads from the main thread The native API I'm using provides a function for this purpose:

HRESULT CancelBlockingCall(DWORD ThreadID)

Although the documentation for the CancelBlockingCall() isn't terribly clear, I believe I need to pass the ThreadID for the OS-level thread which is blocking on the call. Based on the return codes I'm getting from CancelBlockingCall(), I realized that Thread.ManagedThreadID is not what I need. I found the following on msdn (see the Note):

An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the CLR Hosting API to schedule many managed threads against the same operating system thread, or to move a managed thread between different operating system threads.

Does this mean that there is no way for me to properly call CancelBlockingCall() for a managed thread? Is it impossible to determine the ThreadId of the OS-level thread in which a managed thread is currently executing?

解决方案

Does this mean that there is no way for me to properly call CancelBlockingCall() for a managed thread? Is it impossible to determine the ThreadId of the OS-level thread in which a managed thread is currently executing?

As Aidan said, you can use the GetCurrentThreadID API to get the OS thread ID.

To keep track of it across managed threads, you could wrap your API calls in a class where you store the OS Thread ID, so that you can stop it later :

public class APITask
{
    private uint _osThreadId;

    public void Run()
    {
        _osThreadId = GetCurrentThreadID();
        API.RunBlockingMethod();
    }

    public void Cancel()
    {
        API.CancelBlockingCall(_osThreadId);
    }
}

这篇关于ManagedThreadID和操作系统的ThreadID的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆