How to use GC.KeepAlive() and for what purpose?

这一生的挚爱 提交于 2019-12-06 18:51:50

问题


How to use GC.KeepAlive() and for what purpose?

I transfer file from terminal (Windows-CE) to Server using Socket. The time that need to transfer is 8 minutes.A fter 5 minutes the Windows-CE shut-down (if touch screen not pressed)

If I use GC.KeepAlive() for this, does this my problem?


回答1:


You very rarely need to use GC.KeepAlive - very rarely. You'd use it if you wanted to prevent the side-effect of a finalizer from occurring too early, for example. I've most often seen it used for Mutex - keeping mutex alive until the end of an app to make sure there's only one instance. (A using statement is actually better here, but that's a separate matter.)

And no, it doesn't sound like it's relevant in your situation.




回答2:


From the MSDN library for .NET:

The purpose of the KeepAlive method is to ensure the existence of a reference to an object that is at risk of being prematurely reclaimed by the garbage collector. A common scenario where this might happen is when there are no references to the object in managed code or data, but the object is still in use in unmanaged code such as Win32 APIs, unmanaged DLLs, or methods using COM.

So not this would not solve your problem. In fact it's not even related to your problem. The only thing you can do is in the service/application running on the Windows CE, adding code that prevents the system to shutdown as long as the transfer is in progress




回答3:


The screen powering down is a power-management setting on the device. It's going to happen whether or not any app is running. The GC has absolutely nothing to do with this.

If you want to prevent the power maanger from putting hte device into a low-power state, you have a few options. You can periodically call SystemIdleTimerReset in your app.

You could force the power state to something you like with SetSystemPowerState.

You could change the power manager timeouts to something more to your liking by adjusting settings at [HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Power\Timeouts] (followed by a WM_SETTINGS_CHANGE broadcast IIRC).

The best "solution" is going to depend on your app requirements, your users' requirements and the power management system of the target device.



来源:https://stackoverflow.com/questions/7295195/how-to-use-gc-keepalive-and-for-what-purpose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!