从线程单线程单元线程声明资源/内存
我正在使用以下单线程公寓。 我无法从线程对象回收内存/其他资源。 实际上我想将我的线程包装在 try catch 和 fianlly 块中。 try 和 catch 已完成。 但我不确定最终块。 我需要在finally 块中调用哪些代码、属性或函数。
System.Threading.Thread myThread = null;
try
{
myThread = new System.Threading.Thread(functionAddressETC)
myThread .SetApartmentState(System.Threading.ApartmentState.STA);
myThread .Start();
myThread .Join();
}
catch(Exception ex)
{}
finally
{
// I need help in finally block. I need to reclaim all my resources
//what function do i need to call here??????
}
I am using following single threaded appartment.
I am unable to reclaim memory/other resources from thread object.
Actullay I want to wrap my thread in try catch and fianlly block.
try and catch are done. But I am unsure about finally block.
What code, property or function do I need to call in finally block.
System.Threading.Thread myThread = null;
try
{
myThread = new System.Threading.Thread(functionAddressETC)
myThread .SetApartmentState(System.Threading.ApartmentState.STA);
myThread .Start();
myThread .Join();
}
catch(Exception ex)
{}
finally
{
// I need help in finally block. I need to reclaim all my resources
//what function do i need to call here??????
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GC 将回收内存,终结器将释放本机资源。 确保这些过程发生的唯一方法是确保不再需要的对象未被引用。
线程除了其堆栈之外没有任何自己的内存/资源,这些堆栈将通过关闭线程的 Join 来清理(除非您有重复的本机线程句柄的问题:在这种情况下,当最后一个句柄关闭时)。
The GC will reclaim memory, finalizers will free native resources. The only way to ensure these processes happen is to ensure that objects you no longer need are unreferenced.
A thread does not have any memory/resources of its own apart from its stack, which will be cleaned up by the Join shutting down the thread (unless you have something messing with duplicating native thread handles: in which case when the last handle is closed).