Recent

Author Topic: Add a unique id within exe  (Read 21206 times)

timcs

  • Full Member
  • ***
  • Posts: 213
Add a unique id within exe
« on: February 25, 2015, 10:09:47 am »
Hi

   I have wrote a program which starts the mstsc process waits for it and then closes when the connection is complete. There are times when for whatever reason the mstsc crashes and leaves my program stuck , however as there could be multiple instances of my program running for each connection, I wondered if there was a way of adding a unique id within each process that could then be found when the next instance starts and do checks from there.


Thanks

TimCS

flamer0n

  • Guest
Re: Add a unique id within exe
« Reply #1 on: February 25, 2015, 10:37:13 am »
I don't know if this helps, but If i had the same situation - having my program stuck waiting for other process to respond , I would add a sleep(1000) within the loop that waits for response and check for a timeout, if it passed let's say 30secs, I'd break the loop. As for your question "adding a unique id within each process" I have no clue

timcs

  • Full Member
  • ***
  • Posts: 213
Re: Add a unique id within exe
« Reply #2 on: February 25, 2015, 10:47:11 am »
I don't know if this helps, but If i had the same situation - having my program stuck waiting for other process to respond , I would add a sleep(1000) within the loop that waits for response and check for a timeout, if it passed let's say 30secs, I'd break the loop. As for your question "adding a unique id within each process" I have no clue

Hi Flamer0n

Thanks for your quick reply, on your suggestion there is no loop to wait for the process as I am using the TProcess object which auto closes down when the process it starts finishes so I would not know how to do as you suggested.

Thanks

TimCS

balazsszekely

  • Guest
Re: Add a unique id within exe
« Reply #3 on: February 25, 2015, 10:55:08 am »
Hi timcs,

Did you try TUniqueInstance?

http://wiki.lazarus.freepascal.org/UniqueInstance

regards,
GetMem

flamer0n

  • Guest
Re: Add a unique id within exe
« Reply #4 on: February 25, 2015, 10:59:00 am »
Oh, ok , I had this in mind when I replied:
Code: [Select]
I:=0;
 Process1.Execute;
 //give proc some time to start
 Sleep(2000);
 while Process1.Active do
  begin
    //waiting for other process

    Sleep(1000);
    Inc(I,1);
   if (I>59) then break;       

  end; 
I probably misunderstood. Sorry

timcs

  • Full Member
  • ***
  • Posts: 213
Re: Add a unique id within exe
« Reply #5 on: February 25, 2015, 11:27:03 am »
Hi timcs,

Did you try TUniqueInstance?

http://wiki.lazarus.freepascal.org/UniqueInstance

regards,
GetMem

Hi GetMem

  does this mean I can only have once instance of my program running ay any one time? If so it would be no good.

Thanks

TimCS

timcs

  • Full Member
  • ***
  • Posts: 213
Re: Add a unique id within exe
« Reply #6 on: February 25, 2015, 11:28:32 am »
Oh, ok , I had this in mind when I replied:
Code: [Select]
I:=0;
 Process1.Execute;
 //give proc some time to start
 Sleep(2000);
 while Process1.Active do
  begin
    //waiting for other process

    Sleep(1000);
    Inc(I,1);
   if (I>59) then break;       

  end; 
I probably misunderstood. Sorry

No problem I might look at this also but the way the TProcess object I use does not require a loop to wait for the mstsc process to finish.
Thanks

TimCS

balazsszekely

  • Guest
Re: Add a unique id within exe
« Reply #7 on: February 25, 2015, 11:53:56 am »
Quote
does this mean I can only have once instance of my program running ay any one time? If so it would be no good
Yes it does and I misunderstood you!

Each process has its own unique id, you can communicate between processes using pipes, sockets,  mapped files, etc. To be perfectly honest I still don't understand what are you after.


timcs

  • Full Member
  • ***
  • Posts: 213
Re: Add a unique id within exe
« Reply #8 on: February 25, 2015, 11:56:32 am »
Quote
does this mean I can only have once instance of my program running ay any one time? If so it would be no good
Yes it does and I misunderstood you!

Each process has its own unique id, you can communicate between processes using pipes, sockets,  mapped files, etc. To be perfectly honest I still don't understand what are you after.

No problem GetMem, basically my program can be open several times but I need to be able to identify each process that runs by a unique id if possible so I can search for that process by that id.

Hope that is clearer :)

Thanks

TimCS

balazsszekely

  • Guest
Re: Add a unique id within exe
« Reply #9 on: February 25, 2015, 12:11:38 pm »
Quote
Hope that is clearer
Yes, thank you!

Quote
No problem GetMem, basically my program can be open several times but I need to be able to identify each process that runs by a unique id if possible so I can search for that process by that id.

TProcess.ProcessID is the ID of the process(only valid after TProcess.Execute has been called). As I said in my previous post, you can communicate between processes.

timcs

  • Full Member
  • ***
  • Posts: 213
Re: Add a unique id within exe
« Reply #10 on: February 25, 2015, 01:46:09 pm »
Quote
Hope that is clearer
Yes, thank you!

Quote
No problem GetMem, basically my program can be open several times but I need to be able to identify each process that runs by a unique id if possible so I can search for that process by that id.

TProcess.ProcessID is the ID of the process(only valid after TProcess.Execute has been called). As I said in my previous post, you can communicate between processes.

Thanks GetMem, however  I am not after the TProcess ID I am after getting the ID of my program that starts the process of the ID.

Sorry for the confusion here :)

Thanks

TimCS

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Add a unique id within exe
« Reply #11 on: February 25, 2015, 01:52:10 pm »
Quote
Hope that is clearer
Yes, thank you!

Quote
No problem GetMem, basically my program can be open several times but I need to be able to identify each process that runs by a unique id if possible so I can search for that process by that id.

TProcess.ProcessID is the ID of the process(only valid after TProcess.Execute has been called). As I said in my previous post, you can communicate between processes.

Thanks GetMem, however  I am not after the TProcess ID I am after getting the ID of my program that starts the process of the ID.

Sorry for the confusion here :)

Thanks

TimCS
pass the ID as a command line parameter to the executed process.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Add a unique id within exe
« Reply #12 on: February 25, 2015, 02:05:27 pm »
TProcess has a ProcessID AND your program has one as well: GetProcessID

timcs

  • Full Member
  • ***
  • Posts: 213
Re: Add a unique id within exe
« Reply #13 on: February 25, 2015, 02:27:26 pm »
Hi Taazz

  The program starts with parameters as it is called from an internal web page those parameters provide it with the details to be used with mstsc so how do I then use these parameters to identify that process ?

Hi Engkin

 Thank you for your reply, my question on your suggestion is how do I know that the ID I find is the right one if there are multiple processes open ? my thought would be to somehow use a connection id from the internal web page but I would not know how to assign this to the process so it can be identified

Thanks

TimCS

balazsszekely

  • Guest
Re: Add a unique id within exe
« Reply #14 on: February 25, 2015, 02:58:01 pm »
Since we are on windows.
Code: [Select]
uses JwaTlHelp32, Windows;

function IsProcessRunning(PID: DWORD): Boolean;
var
  pProc: TProcessEntry32;
  pSnap: THandle;
  pBool: BOOL;
begin
  Result := False;
  pProc.dwSize := SizeOf(pProc);
  pSnap := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  pBool := Process32First(pSnap, pProc);
  while Integer(pBool) <> 0 do
  begin   
    {ShowMessage('PName: ' + pProc.szExeFile + sLineBreak +
                'PID: ' + IntToStr(pProc.th32ParentProcessID) + sLineBreak +
                'PARENTID: ' + IntToStr(pProc.th32ParentProcessID));}
    if pProc.th32ProcessID = PID then
    begin
       Result := True;
       Break;
    end;
    pBool := Process32Next(pSnap, pProc);
   end;
   CloseHandle(pSnap);
end;

Just make a PID list with your  processes, you can check which one is running from time to time.   

 

TinyPortal © 2005-2018