a problem on threads
-
hai to all i have created a thread function void ThreadRoute1( void* arg ) { USES_CONVERSION; HANDLE hDir = CreateFile( "C:\\", // pointer to the file name FILE_LIST_DIRECTORY, // access (read/write) mode FILE_SHARE_READ|FILE_SHARE_DELETE, // share mode NULL, // security descriptor OPEN_EXISTING, // how to create FILE_FLAG_BACKUP_SEMANTICS, // file attributes NULL // file with attributes to copy ); FILE_NOTIFY_INFORMATION Buffer[1024]; DWORD BytesReturned; while( ReadDirectoryChangesW( hDir, // handle to directory &Buffer, // read results buffer sizeof(Buffer), // length of buffer TRUE, // monitoring option FILE_NOTIFY_CHANGE_SECURITY| FILE_NOTIFY_CHANGE_CREATION| FILE_NOTIFY_CHANGE_LAST_ACCESS| FILE_NOTIFY_CHANGE_LAST_WRITE| FILE_NOTIFY_CHANGE_SIZE| FILE_NOTIFY_CHANGE_ATTRIBUTES| FILE_NOTIFY_CHANGE_DIR_NAME| FILE_NOTIFY_CHANGE_FILE_NAME, // filter conditions &BytesReturned, // bytes returned NULL, // overlapped buffer NULL// completion routine )) { CTime tm = CTime::GetCurrentTime(); CString helper_txt; switch(Buffer[0].Action) { case FILE_ACTION_ADDED: break; case FILE_ACTION_REMOVED:break; case FILE_ACTION_MODIFIED: break; case FILE_ACTION_RENAMED_OLD_NAME: break; case FILE_ACTION_RENAMED_NEW_NAME: break; } int i=0; do { m_Sec.Lock(); int item = pList1->InsertItem(pList1->GetItemCount(), CString(Buffer[i].FileName).Left(Buffer[i].FileNameLength / 2) ); pList1->SetItemText(item, 1, tm.Format("%Y/%m/%d/ - %H:%M:%S")); i++; m_Sec.Unlock(); } while (!Buffer[i].NextEntryOffset); } } i have called the thre
-
hai to all i have created a thread function void ThreadRoute1( void* arg ) { USES_CONVERSION; HANDLE hDir = CreateFile( "C:\\", // pointer to the file name FILE_LIST_DIRECTORY, // access (read/write) mode FILE_SHARE_READ|FILE_SHARE_DELETE, // share mode NULL, // security descriptor OPEN_EXISTING, // how to create FILE_FLAG_BACKUP_SEMANTICS, // file attributes NULL // file with attributes to copy ); FILE_NOTIFY_INFORMATION Buffer[1024]; DWORD BytesReturned; while( ReadDirectoryChangesW( hDir, // handle to directory &Buffer, // read results buffer sizeof(Buffer), // length of buffer TRUE, // monitoring option FILE_NOTIFY_CHANGE_SECURITY| FILE_NOTIFY_CHANGE_CREATION| FILE_NOTIFY_CHANGE_LAST_ACCESS| FILE_NOTIFY_CHANGE_LAST_WRITE| FILE_NOTIFY_CHANGE_SIZE| FILE_NOTIFY_CHANGE_ATTRIBUTES| FILE_NOTIFY_CHANGE_DIR_NAME| FILE_NOTIFY_CHANGE_FILE_NAME, // filter conditions &BytesReturned, // bytes returned NULL, // overlapped buffer NULL// completion routine )) { CTime tm = CTime::GetCurrentTime(); CString helper_txt; switch(Buffer[0].Action) { case FILE_ACTION_ADDED: break; case FILE_ACTION_REMOVED:break; case FILE_ACTION_MODIFIED: break; case FILE_ACTION_RENAMED_OLD_NAME: break; case FILE_ACTION_RENAMED_NEW_NAME: break; } int i=0; do { m_Sec.Lock(); int item = pList1->InsertItem(pList1->GetItemCount(), CString(Buffer[i].FileName).Left(Buffer[i].FileNameLength / 2) ); pList1->SetItemText(item, 1, tm.Format("%Y/%m/%d/ - %H:%M:%S")); i++; m_Sec.Unlock(); } while (!Buffer[i].NextEntryOffset); } } i have called the thre
hi u can pass a void pointer parameter to thread by _beginthread() function. _beginthread() syntex: _beginthread(threadname, stack_size, void *parameter); in ur case, _beginthread(ThreadRoute1, 0, (void *)filepath); In thread(): void ThreadRoute1( void* arg ) { char *filepath; filepath=arg; ......... ........ } By Parthiban
-
hai to all i have created a thread function void ThreadRoute1( void* arg ) { USES_CONVERSION; HANDLE hDir = CreateFile( "C:\\", // pointer to the file name FILE_LIST_DIRECTORY, // access (read/write) mode FILE_SHARE_READ|FILE_SHARE_DELETE, // share mode NULL, // security descriptor OPEN_EXISTING, // how to create FILE_FLAG_BACKUP_SEMANTICS, // file attributes NULL // file with attributes to copy ); FILE_NOTIFY_INFORMATION Buffer[1024]; DWORD BytesReturned; while( ReadDirectoryChangesW( hDir, // handle to directory &Buffer, // read results buffer sizeof(Buffer), // length of buffer TRUE, // monitoring option FILE_NOTIFY_CHANGE_SECURITY| FILE_NOTIFY_CHANGE_CREATION| FILE_NOTIFY_CHANGE_LAST_ACCESS| FILE_NOTIFY_CHANGE_LAST_WRITE| FILE_NOTIFY_CHANGE_SIZE| FILE_NOTIFY_CHANGE_ATTRIBUTES| FILE_NOTIFY_CHANGE_DIR_NAME| FILE_NOTIFY_CHANGE_FILE_NAME, // filter conditions &BytesReturned, // bytes returned NULL, // overlapped buffer NULL// completion routine )) { CTime tm = CTime::GetCurrentTime(); CString helper_txt; switch(Buffer[0].Action) { case FILE_ACTION_ADDED: break; case FILE_ACTION_REMOVED:break; case FILE_ACTION_MODIFIED: break; case FILE_ACTION_RENAMED_OLD_NAME: break; case FILE_ACTION_RENAMED_NEW_NAME: break; } int i=0; do { m_Sec.Lock(); int item = pList1->InsertItem(pList1->GetItemCount(), CString(Buffer[i].FileName).Left(Buffer[i].FileNameLength / 2) ); pList1->SetItemText(item, 1, tm.Format("%Y/%m/%d/ - %H:%M:%S")); i++; m_Sec.Unlock(); } while (!Buffer[i].NextEntryOffset); } } i have called the thre
struct DataYouWantToPass { CString AString; int ANumber; }; // Now to pass the data. DataYouWantToPass *pData = new DataYouWantToPass; // Set data here. _beginthread(ThreadRoute1, 0, reinterpret_cast<void*>(pData)); // As the thread function. void ThreadRoute1( void* arg ) { DataYouWantToPass *pData = reinterpret_cast<DataYouWantToPass*>(arg); // Blah, blah, blah...
Don't forget to see that the data is deleted when it's no longer needed. Steve -
hi u can pass a void pointer parameter to thread by _beginthread() function. _beginthread() syntex: _beginthread(threadname, stack_size, void *parameter); in ur case, _beginthread(ThreadRoute1, 0, (void *)filepath); In thread(): void ThreadRoute1( void* arg ) { char *filepath; filepath=arg; ......... ........ } By Parthiban
hai parthiban thanks for ur reply. but a small problem is there an error is coming when i do this. can u rectify it. error C2440: '=' : cannot convert from 'void *' to 'char *'
-
struct DataYouWantToPass { CString AString; int ANumber; }; // Now to pass the data. DataYouWantToPass *pData = new DataYouWantToPass; // Set data here. _beginthread(ThreadRoute1, 0, reinterpret_cast<void*>(pData)); // As the thread function. void ThreadRoute1( void* arg ) { DataYouWantToPass *pData = reinterpret_cast<DataYouWantToPass*>(arg); // Blah, blah, blah...
Don't forget to see that the data is deleted when it's no longer needed. Stevehai stephen thanks for ur reply but in my problem i have got the directory path in a edit box control variable. so how can i pass that variable to the thread function please help me to solve this problem :)
-
hai parthiban thanks for ur reply. but a small problem is there an error is coming when i do this. can u rectify it. error C2440: '=' : cannot convert from 'void *' to 'char *'
C++ (unlike C) has to implicit converion from
void*
. Use explicit conversion:char *pFilePath = reinterpret_cast<char*>(arg);
Steve -
hai stephen thanks for ur reply but in my problem i have got the directory path in a edit box control variable. so how can i pass that variable to the thread function please help me to solve this problem :)
-
hai stephen thanks for ur reply but in my problem i have got the directory path in a edit box control variable. so how can i pass that variable to the thread function please help me to solve this problem :)
You have to be very careful. He's why:
// Bad code!!! void ThreadRoute1( void* arg ) { int *pInt = reinerpret_cast<int*>(arg); // Use 'pInt' here... } // Call to start thread. { int v = 1; _beginthread(ThreadRoute1, 0, reinterpret_cast<int*>(&v)); } // After this point "v" no longer exists.
This is a recipe for disaster because for all you know the parent thread can make it to the point where "v" is out of scope before it is accessed in the child thread. This is why I usenew
in my previous example. He's a reworked version.void ThreadRoute1( void* arg ) { int *pInt = reinerpret_cast<int*>(arg); // Use pInt. delete pInt; } // Call to start thread. int *pInt = new int(1); if ( _beginthread(ThreadRoute1, 0, reinterpret_cast<void*>(pInt)) == -1 ) { delete pInt; }
Steve