[PATCH] storescp: Close handles & add execute-sync option

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
eludias
Posts: 6
Joined: Tue, 2005-08-30, 16:18

[PATCH] storescp: Close handles & add execute-sync option

#1 Post by eludias »

Hi,

I don't know if this is the right place for patches, and neither do I know if the patch will get white-space mangled this way, but I'll try anyway :)

First, a little background: I'm working on a DICOM forwarder which receives images and sends them further immediately. I try to use STORESCP.EXE unmodified, and use the '-xcr' and '-xcs' to start, continue and stop forwarding.

However, currently two little thinges go wrong:
- After thousands of images it stops functioning
- The events are send asynchronously which means I sometime get <end of study> events before the last <image received> event.

So find attached a patch which does basically two things:
- On Windows, MSDN indicates that process and thread handled should be closed after use. Failing to do so leaks handles. When executing something after each image received, it leaks them quite rapidly. So close handles after use.
- Asynchronous events work fine for when the event handler is a long running process and doesn't happen too often. This is not the case for handler which queue an action after having received one image. Therefore, add an '--exec-sync' / '-xs' option which runs the commands synchronously.

Could this patch be incorporated?

Code: Select all

--- ../dcmtk-3.5.3/dcmnet/apps/storescp.cxx	2004-04-07 18:58:56.000000000 +0200
+++ dcmnet/apps/storescp.cxx	2005-10-12 12:26:18.000000000 +0200
@@ -147,6 +147,7 @@
 OFList<OFString>   outputFileNameArray;
 static const char *opt_execOnReception = NULL;        // default: don't execute anything on reception
 static const char *opt_execOnEndOfStudy = NULL;       // default: don't execute anything on end of study
+OFBool             opt_execSync = OFFalse;            // default: execute in background
 OFString           lastStudySubdirectoryPathAndName;
 static OFBool      opt_renameOnEndOfStudy = OFFalse;  // default: don't rename any files on end of study
 static long        opt_endOfStudyTimeout = -1;        // default: no end of study timeout
@@ -307,6 +308,7 @@
                                                              "execute command c after having received and\nprocessed one C-STORE-Request message" );
     cmd.addOption(  "--exec-on-eostudy",        "-xcs",  1,  "[c]ommand: string (only w/ -ss)",
                                                              "execute command c after having received and\nprocessed all C-STORE-Request messages that\nbelong to one study" );
+    cmd.addOption(  "--exec-sync",              "-xs",       "execute command in foreground instead of background" );
     cmd.addOption(  "--rename-on-eostudy",      "-rns",      "(only w/ -ss) Having received and processed\nall C-STORE-Request messages that belong to\none study, rename output files according to\na certain pattern" );
     cmd.addOption(  "--eostudy-timeout",        "-tos",  1,  "[t]imeout: integer (only w/ -ss, -xcs or -rns)",
                                                              "specifies a timeout of t seconds for\nend-of-study determination" );
@@ -617,6 +619,8 @@
       app.checkValue(cmd.getValue(opt_execOnEndOfStudy));
     }
 
+    if (cmd.findOption("--exec-sync")) opt_execSync = OFTrue;
+
     if (cmd.findOption("--rename-on-eostudy"))
     {
       app.checkDependence("--rename-on-eostudy", "--sort-conc-studies", opt_sortConcerningStudies != NULL );
@@ -1963,8 +1967,9 @@
 
 static void executeCommand( const OFString &cmd )
     /*
-     * This function executes the given command line. Note that the execution will be
-     * performed in a new process so that it does not slow down the execution of storescp.
+     * This function executes the given command line. The execution will be
+     * performed in a new process which can be run in the background
+     * so that it does not slow down the execution of storescp.
      *
      * Parameters:
      *   cmd - [in] The command which shall be executed.
@@ -2005,6 +2010,14 @@
   if( !CreateProcess(NULL, OFconst_cast(char *, cmd.c_str()), NULL, NULL, 0, 0, NULL, NULL, &sinfo, &procinfo) )
     fprintf( stderr, "storescp: Error while executing command '%s'.\n" , cmd.c_str() );
 
+  if (opt_execSync) {
+    // Wait until child process exits (makes execution synchronous).
+    WaitForSingleObject(procinfo.hProcess, INFINITE);
+  }
+
+  // Close process and thread handles since they can look after themselves.
+  CloseHandle(procinfo.hProcess);
+  CloseHandle(procinfo.hThread);
 #endif
 }
 

Marco Eichelberg
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 1449
Joined: Tue, 2004-11-02, 17:22
Location: Oldenburg, Germany
Contact:

#2 Post by Marco Eichelberg »

Thanks for the bug report and the patch. I have merged the patch into our CVS, so the next toolkit release will contain your extension.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest