fix(screenshot): prevent multiple simultaneous screenshot requests#822
Conversation
- Updated the screenshot service to ignore new requests if a screenshot is already in progress, ensuring that only one instance of the screenshot process runs at a time. - Added debug logging to indicate when a screenshot is already in progress and when a new screenshot instance is started. This change improves the user experience by preventing overlapping screenshot operations. bug: https://pms.uniontech.com/bug-view-357037.html
deepin pr auto review这段代码的修改主要是为了修复一个竞态条件问题,即防止在截图操作进行时(特别是当模态对话框打开时)重复触发新的截图任务。以下是对这段代码的详细审查和改进建议: 1. 代码逻辑与语法审查当前逻辑分析:
语法正确性: 2. 代码质量与改进建议虽然逻辑是正确的,但在代码质量和健壮性方面还有提升空间: 建议一:使用 RAII 机制管理标志位(避免忘记重置) 改进方案:使用 Qt 的 #include <QScopeGuard> // 需要包含头文件
void DBusScreenshotService::TopWindowScreenshot()
{
// ... 日志记录代码 ...
if (m_singleInstance) {
qCDebug(dsrApp) << "Screenshot already in progress, ignoring.";
return;
}
m_singleInstance = true;
// 使用 QScopeGuard 确保函数退出时重置标志位
// 注意:这要求截图操作是同步的,或者你需要更复杂的生命周期管理
// 如果 parent()->topWindowScreenshot() 是异步的,这个 guard 会导致函数返回时标志位即失效,
// 那么你需要将 guard 的管理移到异步回调中。
// 假设当前是同步阻塞调用(如注释提到的 QFileDialog):
auto guard = qScopeGuard([this]() {
m_singleInstance = false;
qCDebug(dsrApp) << "Screenshot instance flag reset.";
});
qCDebug(dsrApp) << "Starting new top window screenshot instance";
parent()->topWindowScreenshot();
qCDebug(dsrApp) << "TopWindowScreenshot method finished.";
}注意:如果 建议二:封装重复逻辑 改进方案: // 在类内部定义一个私有辅助方法
void DBusScreenshotService::startScreenshotTask(std::function<void()> task, const QString &logName) {
if (m_singleInstance) {
qCDebug(dsrApp) << "Screenshot already in progress, ignoring.";
return;
}
m_singleInstance = true;
// 如果是同步阻塞操作:
auto guard = qScopeGuard([this]() { m_singleInstance = false; });
qCDebug(dsrApp) << "Starting new" << logName << "instance";
task();
qCDebug(dsrApp) << logName << "method finished.";
}
// 调用方式
void DBusScreenshotService::TopWindowScreenshot() {
// ... 日志 ...
startScreenshotTask([this](){ parent()->topWindowScreenshot(); }, "top window screenshot");
}3. 代码性能
4. 代码安全
总结总体评价:这是一个高质量的 Bug 修复,准确地解决了模态对话框导致的并发问题。 最终建议:
|
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: dengzhongyuan365-dev, lzwind The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
This change improves the user experience by preventing overlapping screenshot operations.
bug: https://pms.uniontech.com/bug-view-357037.html