My procedure works perfectly when there is another instance of my program running but otherwise it gets stuck in a constant loop. I've tryed all sorts and came here as a last resort.
There's not really much else I can say so here's my code:

{*Finds and ends previous instances of toMediaPlayer*}
procedure EndPrevious();
var PlayerProcess:TProcessEntry32;
    Processes:THandle;
    Complete, debug: Boolean;
    Previous:Cardinal;
begin
  //Takes a snapshot of the currently running processes
  Processes := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
  PlayerProcess.dwSize := SizeOf(TProcessEntry32);

  //Searches for the toMediaPlayer Process
  Process32First(Processes,PlayerProcess);
  complete := False;
  repeat
    if PlayerProcess.szExeFile = 'toMediaPlayer.exe'
      then begin
             if not (PlayerProcess.th32ProcessID = GetCurrentProcessID)
                then begin
                       //Closes the toMediaPlayer Process
                       TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,
                                                PlayerProcess.th32ProcessID),0);
                       Complete := True;
                     end;
           end
      else begin
            Complete := False;
            if Process32Next(Processes,PlayerProcess) = false then  Complete := True; <HERE
           end;
  until Complete = True;
end;
{**}

Thanks for any help you can give

Squall

Because you dont check Process32First, if there is none.. that will be fase and you could save yourself a lot of effort with changing the name of your variable to mean still looking.. so its

finding := Process32First..
while finding do
begin
 if match 
   then ..
 else
    finding = process32Next...
end;
commented: Very useful and quick response +1

Thanks a lot for your help. Your suggestions for the code layout did not help directly but they made it a lot easier to see where the problem lay. The problem was that once it found itself it never got chance to run the process32Next function. So i added an else statement (in bold) which solved my problem.

{*Finds and ends previous instances of toMediaPlayer*}
procedure EndPrevious();
var PlayerProcess:TProcessEntry32;
    Processes:THandle;
    Searching: Boolean;
begin
  //Takes a snapshot of the currently running processes
  Processes := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
  PlayerProcess.dwSize := SizeOf(TProcessEntry32);

  //Searches for the toMediaPlayer Process
  Searching := Process32First(Processes,PlayerProcess);
  while Searching = true do
    if PlayerProcess.szExeFile = 'toMediaPlayer.exe'
      then begin
             if not (PlayerProcess.th32ProcessID = GetCurrentProcessID)
                then begin
                       //Closes the toMediaPlayer Process
                       TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,
                                                PlayerProcess.th32ProcessID),0);
                       Searching := False;
                     end
                [B]else Searching := Process32Next(Processes,PlayerProcess);[/B]
           end
      else Searching := Process32Next(Processes,PlayerProcess);
end;
{**}

Thanks again,

Squall

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.