Surveillance Timer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gayathri81
    New Member
    • Jul 2012
    • 25

    Surveillance Timer

    I have designed a geofence and a sms is generated when the person leaves or enters it. Now I want that when the person is out for "x" seconds, another sms is generated telling that "person" is out for "x" seconds.

    Does anyone have idea. I searched a lot but could not get any info about it.

    I have created a field in database where the time(i.e. x) will be entered. My idea is that a timer needs to be started(I think.....)

    Please help me out.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    When the person leaves the room you could start a timer on a separate thread. This thread can run quietly in the background independently from the main thread of your program.

    You can communicate with this background thread from your main thread thereby being able to pick up on an event generated by the background thread.

    Multithreaded programming is very common but it does add a layer of difficulty to your program.

    Comment

    • gayathri81
      New Member
      • Jul 2012
      • 25

      #3
      Originally posted by weaknessforcats
      When the person leaves the room you could start a timer on a separate thread. This thread can run quietly in the background independently from the main thread of your program.

      You can communicate with this background thread from your main thread thereby being able to pick up on an event generated by the background thread.

      Multithreaded programming is very common but it does add a layer of difficulty to your program.
      Hello Sir

      Thanks a lot for the idea you have given. I want the timer to run in the background and not pause the program, and the development enviroment for both GNU/LINUX and Win32.

      Will this really work?

      I heard there is something like WM_TIMER message in Win32. But I dont know if it will work for 64 bit as my server is 64 bit?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        It should work just fine.

        The trick is to maintain commnications between your main thread and the worker thread so you can control the worker thread.

        The worker thread is just a function you write. There are rules about the number and type of arguments and return values.

        Essentially, you call an operating system functon using the address of your functon as one of the arguments. A thread will eb started and your funtion called. When your function completes, the thread dies.

        While the thred is active it runs at the same time as your man thread (on multi-core systems) or shares time (single processor) with it.

        I suggest you write a trivial test program to learn the technique. I have a C++ example of how to do this using classes and Windows.

        Comment

        • gayathri81
          New Member
          • Jul 2012
          • 25

          #5
          Originally posted by weaknessforcats
          It should work just fine.

          The trick is to maintain commnications between your main thread and the worker thread so you can control the worker thread.

          The worker thread is just a function you write. There are rules about the number and type of arguments and return values.

          Essentially, you call an operating system functon using the address of your functon as one of the arguments. A thread will eb started and your funtion called. When your function completes, the thread dies.

          While the thred is active it runs at the same time as your man thread (on multi-core systems) or shares time (single processor) with it.

          I suggest you write a trivial test program to learn the technique. I have a C++ example of how to do this using classes and Windows.
          Hello Sir

          It would be nice if I could have the C++ example. Meanwhile as you told, I tried this out:

          I have a variable, say x which is equal to the value of the surveillance timer. Let us say x= 60;
          I am outside the geofence.
          I want to start the cpu clock and when it reaches 60 (i.e. the value of x) , it should stop the clock and print something .

          I did the following:
          Code:
          double diff;
          clock_t start = clock();   
          diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC; 
          if(diff==x)
          {
          clock_t end = clock();
          flag = "The person is out";
          }
          But I think that it is wrong. Because what would the clock do, till it reaches 60? It should keep on iterating.

          Could you help me out?
          Last edited by zmbd; Sep 11 '12, 11:36 AM. Reason: (z) added required code tags

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Here you go. Sorry for the delay as I was away.

            Code:
            //Demo of how to communicate with threads
            
            
            #include <iostream>
            using namespace std;
            #include <windows.h>
            
            
            class MyClass
            {
            	public:
            		MyClass();
            		~MyClass();
            		static DWORD WINAPI StartThreadOne(LPVOID in);
            		void LaunchOne();
            		void StopOne();
            		void ExecuteThreadOne();
            		int GetDataOne();
            		void SetDataOne(int in);
            
            	private:
            		HANDLE hThreadOne;
            		HANDLE hEventThreadOne;
            		int DataOne;
            		CRITICAL_SECTION cs;
            };
            
            	MyClass::MyClass() : hThreadOne(0), DataOne(0)
            {
            	InitializeCriticalSection(&cs);
            
            	hEventThreadOne = CreateEvent(
            									0,		//handle cannot be inherited
            									TRUE,   //we will do ResetEvent ouselves
            									FALSE,  //event is unsignaled
            									0       //the event object is unnamed
            								 );
            
            }
            	MyClass::~MyClass()
            {
            	DeleteCriticalSection(&cs);
            	CloseHandle(hThreadOne);
            }
            
            void MyClass::LaunchOne()
            {
            	DWORD threadid;					//to hold the returned thread id
            	HANDLE th = ::CreateThread(
            						NULL,		//use default security
            						0,			//use stack size of calling thread
            						StartThreadOne,   	//the thread
            						this,			//the thread input argument
            						0,			//run immediately
            						&threadid
            						);
            	if (!th)
            	{
            		cout << "CreateThread failed" << endl;
            	}
            
            }
            
            void MyClass::StopOne()
            {
            	::SetEvent(hEventThreadOne);
            }
            
            DWORD WINAPI MyClass::StartThreadOne(LPVOID in)
            {
            	reinterpret_cast<MyClass*> (in) ->ExecuteThreadOne();
            
            	return 0;	//thread completed
            }
            
            void MyClass::ExecuteThreadOne()
            {
            	while (1)
            	{
            		if (WaitForSingleObject(hEventThreadOne, 100) == WAIT_OBJECT_0)
            		{
            			//We have been told to shut down this thread
            			break;
            		}
            		Sleep(500);	//simulate some processing
            		EnterCriticalSection(&cs);
            		DataOne++;
            		LeaveCriticalSection(&cs);
            
            	}
            }
            
            int MyClass::GetDataOne()
            {
            	int rval;
            		EnterCriticalSection(&cs);
            		rval = DataOne;
            		LeaveCriticalSection(&cs);
            		return rval;
            
            }
            
            void MyClass::SetDataOne(int in)
            {
            		EnterCriticalSection(&cs);
            		DataOne = in;
            		LeaveCriticalSection(&cs);
            
            }
            
            
            
            int main()
            {
            	cout << "Starting main()" << endl;
            	MyClass  obj;
            			 obj.LaunchOne();
            
            	int buffer;
            	int loopctr = 0;
            	while (1)
            	{
            		loopctr++;
            		buffer = obj.GetDataOne();
            		cout << buffer << " ";
            		Sleep(500);
            		if (buffer > 20)
            		{
            			obj.SetDataOne(0);
            		}
            		if (loopctr == 9)
            		{
            			obj.StopOne();
            		}
            	}
            
            	return 0;
            }
            
            
            
            /*
            HANDLE CreateThread(
              LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
              DWORD dwStackSize,                        // initial stack size
              LPTHREAD_START_ROUTINE lpStartAddress,    // thread function
              LPVOID lpParameter,                       // thread argument
              DWORD dwCreationFlags,                    // creation option
              LPDWORD lpThreadId                        // thread identifier
            );
            */
            /*
            HANDLE CreateEvent(
              LPSECURITY_ATTRIBUTES lpEventAttributes, // SD
              BOOL bManualReset,                       // reset type
              BOOL bInitialState,                      // initial state
              LPCTSTR lpName                           // object name
            );
            */

            Comment

            • gayathri81
              New Member
              • Jul 2012
              • 25

              #7
              Hello Sir

              Thanks for the mail. As you told I am trying to run the thread in parallel. For this purpose I am trying to make the thread sleep using boost.

              I used this:
              Code:
              boost::this_thread(boost::chrono::milliseconds(50));
              However I am getting an error: unable to resolve identifier chrono.When I try to include the header file
              Code:
              #include <boost/chrono.hpp>
              Then I get an error "cannot find include file <boost/chrono.hpp>
              ".
              Where am I going wrong?
              Last edited by zmbd; Sep 11 '12, 11:37 AM. Reason: (z) Added required code tags

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Is there a chrono.hpp file on your system?

                If so, the path to it needs to be added to your compiler's list of standard places. Probably, there is also a library that needs to be added along wit its path.

                I would write your include as:

                Code:
                #include <chrono.hpp>
                The path to the file would be a defined path in your compiler.

                Comment

                Working...