Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,

In c++ program, i like to call a function of the same program but that has to be execute in background and also in a different process ID...

regards,
sanath
Posted
Updated 16-Sep-11 5:56am
v2
Comments
CPallini 16-Sep-11 8:05am    
What do you mean exactly? You may build a server application that offers functionalities to invoking clients.
sanathbt 19-Sep-11 8:59am    
Ya exactly..
Richard MacCutchan 16-Sep-11 11:58am    
Try using CreateThread().
sanathbt 26-Sep-11 8:35am    
the create thread will execute the function in different thread in the same process. How i want is that the function has to be execute in the background in different process. Me attaching the code below.

In this program, the add() function will run in background process, after the completion of the whole function, the background process will be closed but the original process remains as it is... How to delete that process...


#include "stdafx.h"
#include <time.h>
#include <iostream>
#include <windows.h>
#include <stdlib.h>
using namespace std;


DWORD WINAPI add( LPVOID lpParameter)
{
int a,b,c;
cout<<"a:";
cin>>a;
cout<<endl;
cout<<"b:"<<endl;
cin>>b;
c=a+b;
cout<<"sum(c)="<<c<<endl;
return 0;
}

void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait){}
}

int main ()
{
DWORD dwThreadID;
int n,frequency;
HANDLE hProcess;
PROCESS_INFORMATION processInformation;
STARTUPINFO startupInfo;
cout<<"enter the frequency:";
cin>>frequency;
CreateProcess(TEXT("sameprocess.exe"), NULL, NULL, NULL, FALSE, HIGH_PRIORITY_CLASS|CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &processInformation);
for(int i=0;i<2;i++)
{

HANDLE hThread = CreateThread(0,0,add,0,0,&dwThreadID);
cout<<"hi hello";
wait (frequency);
}
printf ("FIRE!!!\n");
//WaitForSingleObject( processInformation.hProcess, INFINITE );
CloseHandle( processInformation.hProcess );
CloseHandle( processInformation.hThread );
return 0;
}


And my program is not waiting for the user input , give me a solution for this..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900