how to sequence multiple actions of event handler

This is a discussion on how to sequence multiple actions of event handler within the C++ Programming forums, part of the General Programming Boards category; Hello, Am trying write to a txt file using streamwrite as one action and also execute a .bat file, both ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    how to sequence multiple actions of event handler

    Hello,

    Am trying write to a txt file using streamwrite as one action and also execute a .bat file, both as result of button.click 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.

    thnx

    richard

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Huh?

    Code:
    void doOnClick ( void ) {
      system("cmd /c batch.bat");
      ofstream o("file.txt");
      o << "hello world" << endl;
      o.close();
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    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
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.

    Code:
    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];
    	wcscpy(cmdLine,commandLine.c_str());
    
    	// Startup options for the new process
    	STARTUPINFOW si;
    	::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)
             cmdLine,
             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
    	::CloseHandle(pi.hThread);
    	::CloseHandle(pi.hProcess);
    		
    	return exitCode;
    
    }
    Last edited by Cat; 10-24-2006 at 10: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
    Posts
    8,893
    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!
    CornedBee

    "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, 05:17 AM
  4. delegate & event
    By Micko in forum C# Programming
    Replies: 5
    Last Post: 03-08-2004, 04:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21