在主线程检测运行在C#库 [英] Detecting running in Main Thread in C# library

查看:151
本文介绍了在主线程检测运行在C#库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个C#DLL,它会在的WinForms要使用的其他开发商。出于某些原因,我想检测,如果从这个库的方法,是从主(GUI)线程调用,并警告开发者,他(在日志文件中即)做了这样的事。是否有任何合理的方式从主线程检测调用的方法? 记得我有WinForm应用程序的访问权限。

I'm creating a C# dll, which is going to be used by others developers in WinForms. For some reasons, I want to detect, if methods from this library, are called from Main (GUI) Thread and warn developer he has done such a thing (ie. in log file). Is there any reasonable way to detect calling method from main thread? Remember I have no access to WinForm application.

推荐答案

在这种情况下,一个简单的方法是定义一个静态控制,即主UI线程上创建的库程序集。如果要检测,如果该库是从主线程调用,然后使用以下

An easy solution in this case is to declare a static control in the library assembly that is created on the Main UI thread. If you want to detect if the library is called from the main thread, then use the following

if (MyLibraryControl.InvokeRequired)
  //do your thing here

这篇关于在主线程检测运行在C#库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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