Skip to content

Commit 0ca3d6a

Browse files
committed
Fix Occasional errors on copy/move files
1 parent 28163e4 commit 0ca3d6a

4 files changed

Lines changed: 54 additions & 27 deletions

File tree

src/CmdLineContextMenu.cpp

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
// fixed problem moving items across volumes
3737
// Version 1.8.4 (c) Nov 2024 thomas694
3838
// fixed copy/move files progress dialog, smoother progress bar
39+
// Version 1.8.5 (c) Jan 2026 thomas694
40+
// fix occasional errors on copy/move files
3941
//
4042
// DFTContextMenuHandler is free software: you can redistribute it and/or modify
4143
// it under the terms of the GNU General Public License as published by
@@ -80,6 +82,19 @@ CCmdLineContextMenu::CCmdLineContextMenu()
8082
m_idCmdLast = 0;
8183
}
8284

85+
CCmdLineContextMenu::~CCmdLineContextMenu() {
86+
CancelAndWait();
87+
}
88+
89+
void CCmdLineContextMenu::CancelAndWait() {
90+
if (m_moveFuture.valid()) {
91+
m_moveFuture.wait();
92+
}
93+
if (m_copyFuture.valid()) {
94+
m_copyFuture.wait();
95+
}
96+
}
97+
8398
//***************************************
8499
//* Date : 6.2.99
85100
//* Last Modified : 2.10.2000
@@ -1688,25 +1703,29 @@ int CCmdLineContextMenu::EmptyFiles()
16881703

16891704
int CCmdLineContextMenu::StartCopyFilesHere()
16901705
{
1691-
std::packaged_task<int()> task([&]() {
1692-
string szFolderDroppedIn = string(m_szFolderDroppedIn);
1693-
return CopyFilesHere(m_strFilenames, szFolderDroppedIn);
1706+
auto filenames = m_strFilenames;
1707+
auto folder = m_szFolderDroppedIn;
1708+
1709+
m_copyFuture = std::async(std::launch::async, [this, filenames, folder]() {
1710+
return CopyFilesHere(filenames, folder);
16941711
});
1695-
std::future<int> res = task.get_future();
1696-
std::thread(std::move(task)).detach();
1697-
res.wait_for(1s);
1712+
if (m_copyFuture.wait_for(std::chrono::seconds(1)) == std::future_status::ready) {
1713+
return m_copyFuture.get();
1714+
}
16981715
return 1;
16991716
}
17001717

17011718
int CCmdLineContextMenu::StartMoveFilesHere()
17021719
{
1703-
std::packaged_task<int()> task([&]() {
1704-
string szFolderDroppedIn = string(m_szFolderDroppedIn);
1705-
return MoveFilesHere(m_strFilenames, szFolderDroppedIn);
1706-
});
1707-
std::future<int> res = task.get_future();
1708-
std::thread(std::move(task)).detach();
1709-
res.wait_for(1s);
1720+
auto filenames = m_strFilenames;
1721+
auto folder = m_szFolderDroppedIn;
1722+
1723+
m_moveFuture = std::async(std::launch::async, [this, filenames, folder]() {
1724+
return this->MoveFilesHere(filenames, folder);
1725+
});
1726+
if (m_moveFuture.wait_for(std::chrono::seconds(1)) == std::future_status::ready) {
1727+
return m_moveFuture.get();
1728+
}
17101729
return 1;
17111730
}
17121731

@@ -1721,26 +1740,17 @@ void CCmdLineContextMenu::DoEvents()
17211740
double diff = difftime(now, m_lastExec);
17221741
if (diff < 10) return;
17231742
m_lastExec = now;
1724-
17251743
MSG msg;
1726-
BOOL result;
17271744

1728-
while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
1745+
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
17291746
{
1730-
result = GetMessage(&msg, NULL, 0, 0);
1731-
if (result == 0)
1747+
if (msg.message == WM_QUIT)
17321748
{
17331749
PostQuitMessage(msg.wParam);
17341750
break;
17351751
}
1736-
else if (result == -1)
1737-
{
1738-
}
1739-
else
1740-
{
1741-
TranslateMessage(&msg);
1742-
DispatchMessage(&msg);
1743-
}
1752+
TranslateMessage(&msg);
1753+
DispatchMessage(&msg);
17441754
}
17451755
}
17461756

@@ -1750,6 +1760,8 @@ int CCmdLineContextMenu::CopyFilesHere(StringArray strFilenames, string szFolder
17501760
lFiles = strFilenames.size();
17511761
lDoneItems = lTotalItems = 0;
17521762

1763+
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1764+
17531765
IProgressDialog* pProgressDlg;
17541766
HRESULT hr = CoCreateInstance(CLSID_ProgressDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pProgressDlg));
17551767
if (FAILED(hr)) pProgressDlg = NULL;
@@ -1805,6 +1817,7 @@ int CCmdLineContextMenu::CopyFilesHere(StringArray strFilenames, string szFolder
18051817
pProgressDlg->Release();
18061818
}
18071819

1820+
CoUninitialize();
18081821
return 1;
18091822
}
18101823

@@ -1933,6 +1946,8 @@ int CCmdLineContextMenu::MoveFilesHere(StringArray strFilenames, string szFolder
19331946
lFiles = strFilenames.size();
19341947
lDoneItems = lTotalItems = 0;
19351948

1949+
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1950+
19361951
IProgressDialog* pProgressDlg;
19371952
HRESULT hr = CoCreateInstance(CLSID_ProgressDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pProgressDlg));
19381953
if (FAILED(hr)) pProgressDlg = NULL;
@@ -1953,6 +1968,11 @@ int CCmdLineContextMenu::MoveFilesHere(StringArray strFilenames, string szFolder
19531968
bool isSameVolume = true;
19541969
if (lFiles > 0) {
19551970
HANDLE hFile = CreateFile(strFilenames[0].c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1971+
if (hFile == INVALID_HANDLE_VALUE) {
1972+
if (pProgressDlg) { pProgressDlg->Release(); }
1973+
CoUninitialize();
1974+
return -1;
1975+
}
19561976
BY_HANDLE_FILE_INFORMATION fileInfo = {};
19571977
BOOL ret = GetFileInformationByHandle(hFile, &fileInfo);
19581978
HANDLE hFile2 = CreateFile(szFolderDroppedIn.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
@@ -2011,5 +2031,6 @@ int CCmdLineContextMenu::MoveFilesHere(StringArray strFilenames, string szFolder
20112031
pProgressDlg->Release();
20122032
}
20132033

2034+
CoUninitialize();
20142035
return 1;
20152036
}

src/CmdLineContextMenu.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <string>
1111
#include <vector>
1212
#include <shlwapi.h>
13+
#include <future>
1314

1415
#define ID_MENU_ITEM 0
1516

@@ -25,6 +26,7 @@ class ATL_NO_VTABLE CCmdLineContextMenu :
2526
{
2627
public:
2728
CCmdLineContextMenu();
29+
~CCmdLineContextMenu();
2830

2931
// IContextMenu interface
3032
STDMETHOD(QueryContextMenu)(HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags);
@@ -79,12 +81,16 @@ END_COM_MAP()
7981
int MoveFilesHere(StringArray strFilenames, string szFolderDroppedIn);
8082
int StartCopyFilesHere();
8183
int StartMoveFilesHere();
84+
void CancelAndWait();
8285

8386
string m_strFileName;
8487
UINT m_idCmdFirst;
8588
UINT m_idCmdLast;
8689
StringArray m_strFilenames;
8790

91+
std::future<int> m_copyFuture;
92+
std::future<int> m_moveFuture;
93+
8894
void DeleteEmptySubfolders(string baseFolder);
8995
bool CheckSubfolders(string folder);
9096
void FlattenTree(string baseFolder, bool useFolderNames);

src/CmdLineExt.rc

0 Bytes
Binary file not shown.

src/app.manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
22
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
3-
<assemblyIdentity type='win32' name='DFTContextMenuHandler' version='1.8.4.0' processorArchitecture="amd64" />
3+
<assemblyIdentity type='win32' name='DFTContextMenuHandler' version='1.8.5.0' processorArchitecture="amd64" />
44
<asmv3:application>
55
<asmv3:windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
66
<ws2:longPathAware>true</ws2:longPathAware>

0 commit comments

Comments
 (0)