Thread: Thread paused when switching tasks

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    193

    Thread paused when switching tasks

    I dont know much about threads, so I'm not sure this is the exact cause for my problem. Anyway, I have this upload form type thing in my application which uploads files to my site. When they hit upload, it opens a progress dialog box and in this progress dialog box, I create a thread using CreateThread which points to a function that uploads the files and increases the progress bar. Everything works fine unless the user switches tasks (for example, switching to an open Internet Explorer or whatever else program that they have opened or are opening). Once this happens, the thread pauses (or it seems like it pauses because the progress bar stops and it stops the upload). So my questions are, is what I'm describing possible and if so, how would I fix this? Here is my CreateThread function if that helps:

    Code:
    DWORD dwID;
    HANDLE hThread = CreateThread(NULL, 0, upload_files, hWndDlg, 0, &dwID);
    CloseHandle(hThread);
    Thanks in advance for any help!!!

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Try to use security descriptor, even if it doesn't seem to help at first.
    Code:
    DWORD dwID;
    SECURITY_ATTRIBUTES threadSID;
    threadSID.nLength=sizeof(SECURITY_ATTRIBUTES);
    threadSID.lpSecurityDescriptor=NULL;
    threadSID.bInheritHandle=TRUE;
    HANDLE hThread = CreateThread(&threadSID, 0, upload_files, hWndDlg, 0, &dwID);
    CloseHandle(hThread);
    Could you post some more code?
    Maybe the thread somehow "depends" on the main window.
    What is hWndDlg doing in the thread?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    two typical threads are worker and ui. on windows platform an instance of an application resides in a process. the process has a default main ui thread. update ui objects from within the ui thread, not the worker thread.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    For simplicity, here is my upload_files function (this isnt exactly it, but there isnt any problem with it, so here are just the basics):

    Code:
    DWORD WINAPI upload_files(HWND hWndDlg) {
         if(error in upload anywhere) {
              error(GetParent(hWndDlg), error); // my own custom error function that uses the first argument to center the error dialog box in the program
              return 0;
         }
         return 1;
    }
    Once again for simplicity, here is the basics to my WndProc (the window procedure to the program):

    Code:
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {
         switch(HIWORD(Message)) {
              case WM_COMMAND:
                   switch(LOWORD(Message)) {
                        case WM_UPLOAD:
                             DialogBox(hInst, "PROGRESSDLG", hWnd, DlgProg);
                             break;
                   }
                   return true;
         }
    }
    For simplicity, once again, here is the DlgProg function:

    Code:
    INT_PTR CALLBACK DlgProg(HWND hWndDlg, UINT Message, WPARAM wParam, LPARAM lParam) {
         switch(Message) {
              case WM_INITDIALOG:
                   // create progress bar
                   DWORD dwID;
                   HANDLE hThread=CreateThread(NULL, 0, upload_files, hWndDlg, 0, &dwID);
                   CloseHandle(hThread);
                   return true;
         }
    }
    Do you need any more code? I think thats about all that has to do with the upload portion. Thanks for the help.

    And for now, I tested that security descriptor code and I havent had a problem yet. I'm going to keep trying to see if I can get it to stop like it did before, but I think that was the trick.

    Thanks for your help.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    I feel like an idiot now. It had nothing to do with the thread or switching tasks or none of that. It had to do with the upload timing out. For instance, if the file was too big that upload would timeout and cause the upload to stop. I was able to fix this by calling a set timeout function for my upload.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  3. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 10:55 AM
  4. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM