是否可以调用 WriteFile 并且应用程序将永远等待回调? [英] Is it possible to call WriteFile and the application will wait for the callback forever?

查看:26
本文介绍了是否可以调用 WriteFile 并且应用程序将永远等待回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用方法 win32::WriteFile 然后我调用

I call the method win32::WriteFile and then I call

WaitForSingleObject( handle, INFINITE )

使用我在 WriteFile 调用中使用的相同句柄.

with the same handle that I used in the WriteFile call.

有没有可能有一些场景让我永远等待......而WriteFile不会完成他的写作?我要写 512 kB 并且我假设我的硬件没有问题.

Is it possible to have some scenario so that I will wait forever... and WriteFile won't finish his writing? I am going to write 512 kB and I'm assuming my hardware has no problem.

我希望获得 FAIL 而不是永远等待.

I expect to get FAIL and not to wait forever.

推荐答案

当您进行重叠的 I/O 调用时,您将责任委托给驱动程序以确保它及时完成它.如果驱动程序有错误或硬件出现故障,您将无能为力,当此类低级操作失败时,任何程序都无法继续以有用的方式运行.

When you make an overlapped I/O call then you delegate responsibility to the driver to ensure it completes it in a timely manner. There's nothing reasonable you can do if the driver has a bug or the hardware is dysfunctional, no program can continue to operate in useful way when such low-level operations fail.

请注意,您已经传递了一个指向驱动程序应该填充的缓冲区的指针,在超时到期后继续运行会留下一个悬空指针,驱动程序可能使用该指针来喷射字节当它确实设法完成请求时进入您的流程.你至少必须调用 CancelIo() 来解决这个问题.如果返回 false 则终止进程.

Beware that you've passed a pointer to a buffer that the driver is supposed to fill, continuing to run after the timeout expires leaves a dangling pointer, one that the driver may use to spray bytes into your process when it actually does manage to complete the request. You must at least call CancelIo() to solve that. And terminate the process if it returns false.

这一切都没有意义,您需要操作系统提供最低限度的服务保证.您不妨在没有 OVERLAPPED 的情况下调用 WriteFile().那个有问题的驱动程序现在将挂起您的程序.正如 CancelIo() 不太可能挂起一样.没关系,是司机有问题,而不是你.不是用户不会注意到机器出现严重问题,其他程序也会遇到这种事故.

This all makes little sense, you need a minimum service guarantee from the operating system. You might as well call WriteFile() without OVERLAPPED. That same buggy driver will now hang your program. Just as it is not unlikely that CancelIo() will hang. Which is okay, it is the driver that has the problem, not you. It's not like the user won't notice that there's something seriously wrong with the machine, other programs will suffer from this kind of mishap as well.

不要编写处理极不可能的极端情况的代码.最重要的是,不要编写无法有效测试的代码.

Don't write code that deals with a very unlikely corner-case. And most of all, don't write code that you can't effectively test.

这篇关于是否可以调用 WriteFile 并且应用程序将永远等待回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆