如何将C++ dll 在 C# 窗口窗体应用程序下的工作线程中运行
How to put C++ dll running in Worker Thread under C# Window Form application
我喜欢在工作线程下运行C++dll,以便C#应用程序的窗口窗体(窗体上的那些按钮(仍然可用。现在,一旦 dll 正在运行,就无法单击 C# 应用程序中 Windows 窗体上的按钮。
我C++导出了成员函数的类。
class Soln {
public:
//Export functions
Soln();
~Soln();
voidgetObjectInformation(void);
void setProcessOver(void);
};
/************These export funcitons are created*********/
extern "C"
{
__declspec(dllexport) Soln* Soln_Create() {
return new Soln();
}
__declspec(dllexport) void Soln_getObjectInformation(Soln* bsn) {
return bsn->getObjectInformation();
}
__declspec(dllexport) void Soln_setProcessOver(Soln* bsn) {
bsn->setProcessOver();
return;
}
__declspec(dllexport) void Soln_delSoln(Soln* bsn) {
bsn->~Soln();
return;
}
}
在 C# 中,它使用 PInvoke
进行接口。
public partial class Form1 : Form
{
[DllImport("Soln_Cpp_Dll.dll", EntryPoint = "Soln_Create")]
public static extern IntPtr Soln_Create();
[DllImport("Soln_Cpp_Dll.dll", EntryPoint = "Soln_delSoln")]
public static extern void Soln_delSoln(IntPtr bsn);
[DllImport("Soln_Cpp_Dll.dll", EntryPoint = "Soln_getObjectInformation")]
public static extern IntPtr Soln_getObjectInformation(IntPtr bsn);
[DllImport("Soln_Cpp_Dll.dll", EntryPoint = "Soln_setProcessOver")]
public static extern void Soln_setProcessOver(IntPtr bsn);
//Variables
IntPtr Soln;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Soln = Soln_Create();
}
private void button4_Click(object sender, EventArgs e)
{
Soln_delBaggageSoln(Soln);
}
}
现在的问题是一旦dll运行,我就无法单击按钮4停止。
我搜索并发现我可以在 C# 中创建一个工作线程。
Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
但这只是创建一个新对象,因为我期待一个返回IntPtr Soln
,如何在工作线程下运行 dll?
若要解决您的问题,请在单击按钮时在 c# 中触发任务并忘记它。
Task.Factory.StartNew(() => Soln_delBaggageSoln(Soln)); //for example
这样,当 dll 函数运行时,您的 UI 线程不会被阻止,因为它会在其他线程中运行。
相关文章:
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- .exe应用程序在windows10中创建新模块时抛出错误,但在windows7中工作正常
- OpenGL应用程序在不同的计算机上的工作方式不同
- 如何将C++ dll 在 C# 窗口窗体应用程序下的工作线程中运行
- Visual Studio 中的套接字 c++ 应用程序无法通过两台计算机之间的以太网连接工作
- 如何将子窗口(MFC 应用程序)的工作区保存到图像
- 为什么我需要运行一个应用程序作为Papi库工作的根源
- 从 WebAPI 项目运行静态类时出现 StackOverflow 异常 - 从控制台应用程序运行时工作正常
- 当我跳到应用程序时,中断无法工作(STM32)
- 如何使用工作线程正确退出Qt应用程序
- 运行在DLL中的工作线程在应用程序关闭时被终止,然后才能正常关闭它们
- 套接字应用程序无法正常工作
- C++使用Qt库的应用程序在屏幕保护程序启动时停止工作
- 在我的Win32应用程序中使TAB键起工作
- 什么可能导致应用程序在工作几个小时后锁定获取 SIGABRT 失败
- 如何使用特定的工作目录运行/调试Netbeans平台应用程序
- 绘制移动图标,它一直是最顶端的鼠标光标和工作的全屏应用程序
- SQL SP在SSMS中正常工作,但在VC++应用程序中失败
- boost::asio 中的未经请求的消息使应用程序崩溃,没有 SSL 它可以正常工作,为什么
- MacOSX C++带有命令行应用程序的工作目录