c# - 在异步线程中分配对象?
问题描述
我正在使用托管 C++ 和 C#。我对以下代码有疑问。
//In C++, Run on background thread (not UI Thread)
while(true)
{
BitmapSource^ bitmapSource = CreateBitmap();
bitmapSource->Freeze();
CSWrapperFuntion(bitmapSource); //this is a function pointer to C#
}
//In C#, Interface Project
void CSWrapperFuntion(Object param)
{
mainWindow.Dispatcher.BeginInvoke(new Action(delegate()
{
BitmapSource bitmap = (BitmapSource)param;
if (bitmap != null)
{
//Use this bitmap to display on UI Control
}
}));
}
当我在异步线程(UI线程)中赋值BitmapSource bitmap = (BitmapSource)param;
时,使用这个位图对象有什么问题吗?
在 BeginInvoke 的主体中,可能 Object 参数超出范围,是 GC 销毁?
我对 C# 的这种情况一无所知,所以我无法猜测将来会出现任何问题。
有人可以告诉我这段代码中的潜在错误吗?
非常感谢!
解决方案
推荐阅读
- javascript - 从 d3 事件处理程序访问类函数
- java - Spring 安全休息登录
- c - 使用C逐字节读取文件
- google-app-engine - 无法使用 spring cloud gcp starter 将 Spring Boot 与 Cloud SQL 连接
- c - C: sem_open 上的“没有这样的文件或目录”,带有 O_CREAT 和正确的信号量名称
- javascript - 可以在浏览器中加载外部库吗?
- javascript - 在散点图中设置 X 轴的最小日期 ChartJS
- android - DAO 在特定时间后停止插入数据库
- node.js - Mongoose 模型中的预钩子作为 ES6 类
- java - 在 Mac OS X 上修补 Java 应用程序包的最佳方法是什么?