Рейтинг:  0 / 5

Звезда не активнаЗвезда не активнаЗвезда не активнаЗвезда не активнаЗвезда не активна
 
Создание отдельного потока предотвращает подвисание обновления интерфейса программы в т.ч. “бегунка” прогресс-бара (компилятор PellesC):
Функция инициализации потока:
void makeThreadConcate(void){
    HANDLE hThread;
    DWORD  ThreadId;
    BOOL   Active;
    hThread = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)ConcateFiles,
                           (void*)&Active, 0, &ThreadId); // в Active – состояние потока
 
    while(Active == TRUE);
    CloseHandle(hThread);
 
}
 
Функция сращивания трёх файлов: там же и управление контролом прогресс-бара с помощью сообщений.
 
extern HWND hwndDlgG;
DWORD WINAPI ConcateFiles(LPVOID lpParam){
char buffer[256]={'\0'};
HWND hEdit;
HANDLE hWriteFile, hReadFile1, hReadFile2;
unsigned long dwBytesReadWrite=0;
signed short WritedNumber1,WritedNumber2=0;
unsigned long Counter=0;
int ProgressX=0;
////////////////////THREAD SUITE//////////////////////////////////
    BOOL *Active;
    Active = (BOOL*)lpParam;
    *Active = TRUE;    // флаг активности потока
//////////////////////////////////////////////
hEdit = GetDlgItem(hwndDlgG,4004);
GetWindowText(hEdit,(LPSTR)buffer,256);
 
CopyFile(buffer,"TensoCurve.ad",0);
 
hWriteFile = CreateFile("TensoCurve.ad", GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 
hEdit = GetDlgItem(hwndDlgG,4002);
GetWindowText(hEdit,(LPSTR)buffer,256);
hReadFile1 = CreateFile(buffer, GENERIC_READ,  FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 
hEdit = GetDlgItem(hwndDlgG,4005);
GetWindowText(hEdit,(LPSTR)buffer,256);
hReadFile2 = CreateFile(buffer, GENERIC_READ,  FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 
DWORD nSize = GetFileSize (hReadFile1, 0);
 
while(ReadFile(hWriteFile,  &WritedNumber1, 2, &dwBytesReadWrite, NULL)){
            if (!dwBytesReadWrite) break;
            }; // ищем конец;
 
while(ReadFile(hReadFile1,  &WritedNumber1, 2, &dwBytesReadWrite, NULL)&&ReadFile(hReadFile2,  &WritedNumber2, 2, &dwBytesReadWrite, NULL)){
            if (!dwBytesReadWrite) break;
            WriteFile(hWriteFile,&WritedNumber2 ,2,&dwBytesReadWrite,NULL);
                        WriteFile(hWriteFile,&WritedNumber1 ,2,&dwBytesReadWrite,NULL);
                       
                        if(Counter*1000/(nSize>>1)!= ProgressX){ ProgressX=(int)(Counter*1000/(nSize>>1));
SendMessage(GetDlgItem(hwndDlgG,4001), PBM_SETPOS, ProgressX, 0);                                                                                                                                                      
                                   }
                                   Counter++;
            };
 
CloseHandle(hReadFile1);
CloseHandle(hReadFile2);
CloseHandle(hWriteFile);
SendMessage( GetDlgItem(hwndDlgG,4001), PBM_SETPOS, 0, 0);            // обнуляем прогресс
//////////////////////////////////////////////////////////////
    // Завершение потока
    *Active = FALSE;  // Сбрасываем флаг активности
    ExitThread(0);    // Специальный вызов для закрытия потока
///////////////////////////////////////////////////////////////
return 0;
}
 
Вызов в static INT_PTR CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
 
static INT_PTR CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_INITDIALOG:
                                   //инициализируем прогресс-бар
   hProgress= GetDlgItem(hwndDlg,4001);
 SendMessage(hProgress,PBM_SETRANGE, 0, MAKELPARAM(0, 1000)); // максимальное

//значение прогресс-бара - тысяча
SendMessage(hProgress, PBM_SETSTEP, 1, 0); // - шаг прогресс-бара - единичка          
SendMessage(hProgress, PBM_SETPOS, 0, 0); // ставим позицию прогресс-бара
                   
                                   ////////////////////////////////////////////////////////
            return TRUE;
 
        case WM_SIZE:
            /*
             * TODO: Add code to process resizing, when needed.
             */
            return TRUE;
 
        case WM_COMMAND:
            switch (GET_WM_COMMAND_ID(wParam, lParam))
            {
                /*
                 * TODO: Add more control ID's, when needed.
                 */
                case IDOK:
                        GetFileName(FileName,sizeof(FileName));
                                               hEdit1 =  GetDlgItem(hwndDlg,4002);
                                               if(*FileName!='\0')SendMessage(hEdit1, WM_SETTEXT, 0,(long)FileName); return TRUE; 
                case 4006:
                        GetFileName(FileName,sizeof(FileName));
                                               hEdit1 =  GetDlgItem(hwndDlg,4005);
                                               if(*FileName!='\0')SendMessage(hEdit1, WM_SETTEXT, 0,(long)FileName); return TRUE; 
                           case 4007:
                        GetFileName(FileName,sizeof(FileName));
                                               hEdit1 =  GetDlgItem(hwndDlg,4004);
                                               if(*FileName!='\0')SendMessage(hEdit1, WM_SETTEXT, 0,(long)FileName); return TRUE; 
        //            EndDialog(hwndDlg, TRUE);
                           case 4008:
              hwndDlgG = hwndDlg;
//THREADS ROUTINE///////////////////////////////////
makeThreadConcate();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    return TRUE; 
 
            }
            break;
 
        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;
 
        /*
         * TODO: Add more messages, when needed.
         */
    }
 
    return FALSE;
}