C#功能意识?
在多线程应用程序中。有没有办法通过编程让thread-B检查AD-a当前在哪个函数中?您可以通过以下操作获得另一个线程的堆栈跟踪:C#功能意识?,c#,multithreading,function,C#,Multithreading,Function,在多线程应用程序中。有没有办法通过编程让thread-B检查AD-a当前在哪个函数中?您可以通过以下操作获得另一个线程的堆栈跟踪: System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(myThread); 您可以从中获得调用堆栈及其当前执行的函数。这是应用程序本身应该内置的内容,以避免线程之间可能的争用 在我看来,一个线程不应该控制另一个线程的执行(例如,挂起/恢复),除非启动它。他们应该仅仅
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(myThread);
您可以从中获得调用堆栈及其当前执行的函数。这是应用程序本身应该内置的内容,以避免线程之间可能的争用 在我看来,一个线程不应该控制另一个线程的执行(例如,挂起/恢复),除非启动它。他们应该仅仅建议另一个线程控制自己(例如,使用互斥体和事件)。这大大简化了线程管理,并降低了竞争条件的可能性 如果您真的想让线程B知道线程A当前在做什么,那么线程A应该使用一个受互斥保护的字符串(其中包含函数名)与线程B(或任何其他线程,如下面的主线程)通信。类似于(伪代码)的东西:
此方法可用于支持线程的任何语言或环境,而不仅仅是.Net或C#。线程A中的每个函数都有prolog和epilog代码,用于保存、设置和还原其他线程中使用的值。值得注意的是,我不知道这样做是否会遇到线程问题。另一个问题是需要调用Suspend/Resume。这里有一个类似的问题:
global string threadAFunc = ""
global mutex mutexA
global boolean stopA
function main:
stopA = false
init mutexA
start threadA
do until 20 minutes have passed:
claim mutexA
print "Thread A currently in " + threadAFunc
release mutexA
stopA = true
join threadA
return
function threadA:
string oldThreadAFunc = threadAFunc
claim mutexA
threadAFunc = "threadA"
release mutexA
while not stopA:
threadASub
claim mutexA
threadAFunc = oldThreadAFunc
release mutexA
return
function threadASub:
string oldThreadAFunc = threadAFunc
claim mutexA
threadAFunc = "threadASub"
release mutexA
// Do something here.
claim mutexA
threadAFunc = oldThreadAFunc
release mutexA
return