CreateProcess
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: CreateProcess

  1. #1
    Join Date
    Apr 2009
    Location
    CPH
    Posts
    10

    Angry CreateProcess

    Hello!
    I am making an application that processes multiple files (typically > 500) through a command prompt call. The way I start the command prompt app is by looping using a call to CreateProcess for each file that is to be processed. It works fine, except that I somehow loose 'connection' to my app so that 1: Windows says that the app. is 'Not Responding'
    2: The Cprogress bar in my app is not updated before all files have been processed, even though there is a CreateProcess call and a Cprogress.StepIt() from the app for each file that needs processing.
    I somehow suspect that the CPU gets swamped...
    I do not want that Windows starts to say that my app is 'not responding' and I want my Cprogress dialog bar to update according to the number of files that are progressed through.
    I wonder if multithreading is the OK way to go instead of just kicking of series of CreateProcess calls? Maybe my CreateProcess is not ending correctly? It seems as if my app is 'not regaining control' before very late. The app never crashes though.
    My CreateProcess code is listed below, maybe there can be a problem with it, or maybe I should do things in a different way? My app basicaly works as it never crashes, but with above mentioned problems it is NOT a pro solution... Help much appreciated:-)
    BR's
    Gustav

    void CMultiFilerDlg::ProcessFile(CString pdfFile)
    {
    int i=0;
    DWORD ProcID;
    // Open file in text mode:
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    char cmdArgs[2052];
    ProcID=GetCurrentProcessId();
    si.wShowWindow=SW_HIDE;

    *cmdArgs=NULL;
    sprintf(cmdArgs,"%s", pdfFile.GetBuffer(0));

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    if( !CreateProcess( "C:\\Program Files (x86)\\MyCommandApp.exe",
    cmdArgs, // Command line.
    NULL,
    NULL,
    FALSE,
    CREATE_NO_WINDOW,
    NULL,
    NULL,
    &pi ) )
    {
    int ERR=1;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );




    }

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: CreateProcess

    Quote Originally Posted by Gustav_sf View Post
    I wonder if multithreading is the OK way to go instead of just kicking of series of CreateProcess calls?
    If the program you call using CreateProcess is under you control, then yes, using threads is an easier approach. Look at the FAQ section of the forum; there is something about using worker threads.
    Quote Originally Posted by Gustav_sf View Post
    Maybe my CreateProcess is not ending correctly? It seems as if my app is 'not regaining control' before very late. The app never crashes though.
    You wait for the process to finish. When did you expect your application to regain control?

    Please use code tags when posting code.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,640

    Re: CreateProcess

    Your problem is that you do lengthy processing in the GUI thread. See this link on how to create a worker thread http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,546

    Re: CreateProcess

    So many questions!
    The 'Not Responding' issue is explained by S_M_A. You can fix it with either working threads, or by processing Windows messages from time to time. Use MsgWaitForMultipleObjects() in place of your WaitForSingleObject().
    The ProgressBar doesn’t update, most likely, for the same reason. You can either force it to update by calling Cprogress.UpdateWindow() right after Cprogress.StepIt(), or pump the messages by GetMessage()/DispatchMessage() loop.
    Multithreading certainly should be considered, since you have over 500 similar actions that do not have to be done in sequence (or do they?). The most simple way is to create all processes without waiting for them to finish, and use one MsgWaitForMultipleObjects(). This is likely NOT the most efficient way. Do you know if your MyCommandApp.exe is CPU bound or IO bound?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  5. #5
    Join Date
    Apr 2009
    Location
    CPH
    Posts
    10

    Resolved Re: CreateProcess

    Hi All!
    Thank you for all your replies. Meanwhile (as also suggested by some replies) I redesigned my call to the CreateProcess function through multithreading .
    Unfortunately this did not resolve the 'Not Responding' message.
    Then I decided to address my message queue after each thread completion with a call like:
    PeekMessage(&msg, this->GetSafeHwnd(), 0, 0, PM_REMOVE);

    That did it! No more 'Not Responding' messages and much better control of the app because of the multithreading!
    Not sure if this is the cleanest way to address the problem, but I can just not find a better way..

    BR's
    Gustav

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: CreateProcess

    Quote Originally Posted by Gustav_sf View Post
    Then I decided to address my message queue after each thread completion with a call like:
    PeekMessage(&msg, this->GetSafeHwnd(), 0, 0, PM_REMOVE);

    That did it! No more 'Not Responding' messages and much better control of the app because of the multithreading!
    Not sure if this is the cleanest way to address the problem, but I can just not find a better way..
    I guess you didn't find the FAQ section.
    How to access UI elements from a thread in MFC?

    Note that your solution will only work in good conditions. In conditions where file access can be slow (such as accessing remote files), you may still get the 'not responding' indication.
    Last edited by D_Drmmr; December 28th, 2012 at 10:15 AM.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Apr 2009
    Location
    CPH
    Posts
    10

    Resolved Re: CreateProcess

    Hi Brad,
    Thanks for the input, but the FAQ suggestion you are referring to never worked - tried a lot of things...
    My app is to be global, so I will just prey for it:-)
    BR's
    Gustav

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center