Thread: how to sequence multiple actions of event handler

  1. #1
    Registered User
    Join Date
    Oct 2006

    how to sequence multiple actions of event handler


    Am trying write to a txt file using streamwrite as one action and also execute a .bat file, both as result of event. How can I have some control in the order of how these two are executed, I need to execute the .bat file before creating the txt file. Is there some form of sequence control.



  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe

    void doOnClick ( void ) {
      system("cmd /c batch.bat");
      ofstream o("file.txt");
      o << "hello world" << endl;
    How is it difficult to make these things happen in sequence?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Why it is difficult? Because system() starts the batch file as a new process, which may not be done when the program opens the file.
    A workaround is somewhere in MS Knowledge Base.

  4. #4
    Registered User
    Join Date
    May 2003
    Well, this is windows-specific, but here's the code I use when I need to do something similar. It spawns a process, waits for the process to terminate, and then returns the error code.

    Note: this will cause your program to pause (indefinitely) until the child process returns. That is by design, but this may not be applicable for your needs. In my case it was fine; this was only called once, on program startup, the program's only GUI was a taskbar icon, and the process it spawned would always return within a few seconds, success or fail.

    long SpawnProcess(std::wstring commandLine){
    	// Because the command line can't be a const string, make a copy of the string:
    	wchar_t * cmdLine = new wchar_t[commandLine.length()+1];
    	// Startup options for the new process
    	::ZeroMemory(&si,sizeof(si)); // Zero it all
    	si.cb = sizeof(si);           // Let it know its size
    	// We don't really need to use any of the rest of the structure (which is things like
    	// window size, position, and other crap) so we can leave it all zeroed.
    	PROCESS_INFORMATION pi;  // This will be filled in by CreateProcess
    	if (!::CreateProcessW(
             NULL,   // Application name (we put it in the command line so this can be NULL)
             NULL,   // Security attributes if we want the process handle inheritable
             NULL,   // Security attributes if we want the thread handle inheritable
             FALSE,  // Our child process doesn't need any of our handles.
             CREATE_NO_WINDOW, // Flags
             NULL,   // Environment Variables
             NULL,   // Current directory (NULL = the current directory of our program)
             &si,    // That  startup info struct.
             &pi   // Process information
    		delete[] cmdLine;
    		throw std::runtime_error("SpawnProcess: Create process failed.");
    	delete[] cmdLine;
    	// OK we made the program run.  Let's wait for it to stop.
    	::WaitForSingleObject(pi.hProcess,INFINITE); // You may want a timeout here...
    	// Let's check the error code
    	long exitCode;
    	if (!::GetExitCodeProcess(pi.hProcess,(unsigned long *)&exitCode))
    		throw std::runtime_error("SpawnProcess: Retrieving process exit code failed.");
    	// Close the handles after we get the exit code
    	return exitCode;
    Last edited by Cat; 10-24-2006 at 09:38 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    system() is required to return the exit status of the command, so I don't see how any but a completely broken implementation could not wait until the batch file is complete.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. Opening Multiple Files in sequence
    By wujiajun in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 08:47 PM
  3. Finding controls from an event handler
    By bennyandthejets in forum C# Programming
    Replies: 2
    Last Post: 07-03-2005, 04:17 AM
  4. delegate & event
    By Micko in forum C# Programming
    Replies: 5
    Last Post: 03-08-2004, 04:05 AM