2

I'm looking for a fast alternative to CreateProcess in Delphi to execute certain calculations in an exe, including several return-values in XML. Currently, I'm calling an C#-exe with certain parameters. One of these calls takes approx. 0.5s - which is way to expensive, since this exe needs to be called a couple of hundred times (unfortunatley iterative calls, i.e. multithreading won't speed up the job).

My current code looks like this (found the solution to get the console output of the exe somwhere on StackOverflow).

IsExecutable := CreateProcess(
            nil,
            PChar(WorkDir + Exe + CommandLine),
            nil,
            nil,
            True,
            HIGH_PRIORITY_CLASS,
            nil,
            nil,
            StartupInfo,
            ProcessInformation);
CloseHandle(StdOutPipeWrite);
    if IsExecutable then
      try
        repeat
          WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
          if BytesRead > 0 then
          begin
            Buffer[BytesRead] := #0;
            Result := Result + Buffer;
          end;
        until not WasOK or (BytesRead = 0);
        WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
      finally
        CloseHandle(ProcessInformation.hThread);
        CloseHandle(ProcessInformation.hProcess);
      end

Btw, I'm not very good a Delphi - actually, I kinda feel like the "I have no idea what I'm doing" dog-meme-thing...

0

5 Answers 5

8

Creating a new process is expensive. There are no alternatives to using CreateProcess. There are no quick fixes here. There's no magic button which makes process creation super fast.

The solution to your problem is going to be to stop creating a new process altogether. Perform this task inside your process.

2
  • 1
    Or if process separation has to be maintained for some reason, start it once and keep communicating with it, so you don't have to recreate it repeatedly (there will still be overhead, but much less significant).
    – Thomas
    Sep 27, 2012 at 13:51
  • @Thomas That's true. IPC can be costly though so in-proc may well be the only answer. If the calculation is significant enough then the IPC cost may become negligible. If the calculation is cheap, then IPC may dominate. Sep 27, 2012 at 13:55
3

Do you need to create a new process every time? Or can you re-use the same instance? You can do it with some low level console input/output piping (your code looks like you're trying to do this now :) ), or you can use some kind of IPC (mutex+shared memory, to copy data between 2 processes), or you can use a more n-tier (or client/server) approach by communicating via TCP/IP (RemObjects SDK is very good for this one, has both .Net and Delphi libs, which communicate via TCP/IP, or windows messages, named pipes, etc). However the last approach adds some more delay/overhead because of the abstraction but is much easier to use than to implement both sides in .Net and Delphi yourself (it is not that difficult btw).

1
  • Speaking of IPC, check www.cromis.net/blog/downloads/cromis-ipc/
    – Edwin Yip
    Sep 28, 2012 at 5:38
3

Like David wrote and André wrote, you cannot change this.

This is one of the really big differences between Windows and Linux. CreateProcess in Windows is very expensive, whereas fork in Linux is cheap. On the other hand, a Linux fork is usually followed by an exec which is expensive. This is why .NET introduced the AppDomain which is faster.

There are a couple of directions you could go to integrate the .NET code into your process like Arnaud wrote.

Another thing you can do is do IPC between a Delphi process and a .NET process (for instance using named pipes), and have the .NET process use AppDomains for each request.

4
  • fork isn't much use without the exec Sep 27, 2012 at 17:12
  • actually it is for a lot of servers. Sep 27, 2012 at 19:27
  • That's not what's happening here though Sep 27, 2012 at 21:43
  • Then don't make an absolute statement on an answer that tries to provide some background information on why things are a certain way. AppDomains are somewhere in the middle of fork and CreateProcess. Hence that introduction to the last line of my answer. Sep 28, 2012 at 5:12
1

What is slow here is creating a new process, and much more launching the whole .Net framework process each time you run the execuable.

For best performance, you should better expose your C# process as COM, then call it from Delphi directly.

Remote connection via TCP/IP is not worth it in this case, I guess. I had bad speed experiment about using RemObjects in such case on a local machine, some years ago.

What you need is call COM interop, and you may be interested in:

Delphi handles COM objects very well, and you would be able to call C# methods very easily from Delphi code.

0

Use Thread Pools or IOCP instead? Yeah, it's a little bit complicated and resource consuming, but eliminates the need to execute external processes. The new ThreadPool APIs (Vista and later) give you much more opportunities and flexibility (you can even mark tasks that run for long time):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.