开发者

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

开发者 https://www.devze.com 2023-04-02 00:52 出处:网络
How can we use GC.KeepAlive() and what is the purpose? I transfer files from terminal (Windows-CE) to Server using Socket. The time needed for transfer is 8 minutes. After 5 minutes the Windows-CE shu

How can we use GC.KeepAlive() and what is the purpose?

I transfer files from terminal (Windows-CE) to Server using Socket. The time needed for transfer is 8 minutes. After 5 minutes the Windows-CE shuts down (if touch screen n开发者_如何学Pythonot pressed)

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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号