473,383 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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.
Aug 7 '12 #1

✓ answered 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.

7 2249
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 7 '12 #2
@weaknessforcats
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?
Aug 13 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 13 '12 #4
@weaknessforcats
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:
Expand|Select|Wrap|Line Numbers
  1. double diff;
  2. clock_t start = clock();   
  3. diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC; 
  4. if(diff==x)
  5. {
  6. clock_t end = clock();
  7. flag = "The person is out";
  8. }
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?
Aug 22 '12 #5
weaknessforcats
9,208 Expert Mod 8TB
Here you go. Sorry for the delay as I was away.

Expand|Select|Wrap|Line Numbers
  1. //Demo of how to communicate with threads
  2.  
  3.  
  4. #include <iostream>
  5. using namespace std;
  6. #include <windows.h>
  7.  
  8.  
  9. class MyClass
  10. {
  11.     public:
  12.         MyClass();
  13.         ~MyClass();
  14.         static DWORD WINAPI StartThreadOne(LPVOID in);
  15.         void LaunchOne();
  16.         void StopOne();
  17.         void ExecuteThreadOne();
  18.         int GetDataOne();
  19.         void SetDataOne(int in);
  20.  
  21.     private:
  22.         HANDLE hThreadOne;
  23.         HANDLE hEventThreadOne;
  24.         int DataOne;
  25.         CRITICAL_SECTION cs;
  26. };
  27.  
  28.     MyClass::MyClass() : hThreadOne(0), DataOne(0)
  29. {
  30.     InitializeCriticalSection(&cs);
  31.  
  32.     hEventThreadOne = CreateEvent(
  33.                                     0,        //handle cannot be inherited
  34.                                     TRUE,   //we will do ResetEvent ouselves
  35.                                     FALSE,  //event is unsignaled
  36.                                     0       //the event object is unnamed
  37.                                  );
  38.  
  39. }
  40.     MyClass::~MyClass()
  41. {
  42.     DeleteCriticalSection(&cs);
  43.     CloseHandle(hThreadOne);
  44. }
  45.  
  46. void MyClass::LaunchOne()
  47. {
  48.     DWORD threadid;                    //to hold the returned thread id
  49.     HANDLE th = ::CreateThread(
  50.                         NULL,        //use default security
  51.                         0,            //use stack size of calling thread
  52.                         StartThreadOne,       //the thread
  53.                         this,            //the thread input argument
  54.                         0,            //run immediately
  55.                         &threadid
  56.                         );
  57.     if (!th)
  58.     {
  59.         cout << "CreateThread failed" << endl;
  60.     }
  61.  
  62. }
  63.  
  64. void MyClass::StopOne()
  65. {
  66.     ::SetEvent(hEventThreadOne);
  67. }
  68.  
  69. DWORD WINAPI MyClass::StartThreadOne(LPVOID in)
  70. {
  71.     reinterpret_cast<MyClass*> (in) ->ExecuteThreadOne();
  72.  
  73.     return 0;    //thread completed
  74. }
  75.  
  76. void MyClass::ExecuteThreadOne()
  77. {
  78.     while (1)
  79.     {
  80.         if (WaitForSingleObject(hEventThreadOne, 100) == WAIT_OBJECT_0)
  81.         {
  82.             //We have been told to shut down this thread
  83.             break;
  84.         }
  85.         Sleep(500);    //simulate some processing
  86.         EnterCriticalSection(&cs);
  87.         DataOne++;
  88.         LeaveCriticalSection(&cs);
  89.  
  90.     }
  91. }
  92.  
  93. int MyClass::GetDataOne()
  94. {
  95.     int rval;
  96.         EnterCriticalSection(&cs);
  97.         rval = DataOne;
  98.         LeaveCriticalSection(&cs);
  99.         return rval;
  100.  
  101. }
  102.  
  103. void MyClass::SetDataOne(int in)
  104. {
  105.         EnterCriticalSection(&cs);
  106.         DataOne = in;
  107.         LeaveCriticalSection(&cs);
  108.  
  109. }
  110.  
  111.  
  112.  
  113. int main()
  114. {
  115.     cout << "Starting main()" << endl;
  116.     MyClass  obj;
  117.              obj.LaunchOne();
  118.  
  119.     int buffer;
  120.     int loopctr = 0;
  121.     while (1)
  122.     {
  123.         loopctr++;
  124.         buffer = obj.GetDataOne();
  125.         cout << buffer << " ";
  126.         Sleep(500);
  127.         if (buffer > 20)
  128.         {
  129.             obj.SetDataOne(0);
  130.         }
  131.         if (loopctr == 9)
  132.         {
  133.             obj.StopOne();
  134.         }
  135.     }
  136.  
  137.     return 0;
  138. }
  139.  
  140.  
  141.  
  142. /*
  143. HANDLE CreateThread(
  144.   LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
  145.   DWORD dwStackSize,                        // initial stack size
  146.   LPTHREAD_START_ROUTINE lpStartAddress,    // thread function
  147.   LPVOID lpParameter,                       // thread argument
  148.   DWORD dwCreationFlags,                    // creation option
  149.   LPDWORD lpThreadId                        // thread identifier
  150. );
  151. */
  152. /*
  153. HANDLE CreateEvent(
  154.   LPSECURITY_ATTRIBUTES lpEventAttributes, // SD
  155.   BOOL bManualReset,                       // reset type
  156.   BOOL bInitialState,                      // initial state
  157.   LPCTSTR lpName                           // object name
  158. );
  159. */
  160.  
Sep 6 '12 #6
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:
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. #include <boost/chrono.hpp>
  2. Then I get an error "cannot find include file <boost/chrono.hpp>
  3. ".
Where am I going wrong?
Sep 11 '12 #7
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. #include <chrono.hpp>
The path to the file would be a defined path in your compiler.
Sep 11 '12 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Anto | last post by:
Hello, I'm italian programmer I would have to realize a video program surveillance for cellular and I do not know just from where to begin. Someone has a po' of experience in merit? Practically...
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
12
by: Gina_Marano | last post by:
I have created an array of timers (1-n). At first I just created windows form timers but I read that system timers are better for background work. The timers will just be monitoring different...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
3
by: lagu2653 via DotNetMonster.com | last post by:
I have a log-in window application. When the user presses the cancel button it kills another window by it's name and then it exits. The problem is that if somebody kills the log-in window by...
2
by: paeh | last post by:
Can any expert help me. I need to finish up my final project system next year. Can anyone give me some idea how to code my system. My system details is below : 1.Develop modules for a...
0
by: Rambaldi | last post by:
Hey people, I'm working in a project in video surveillance area, so i got a question: Which video surveillance open-source platforms do you know??? and what do think is the best to work with???...
1
by: Rambaldi | last post by:
What is the best free application to manage a video surveillance system in linux? I'm making a project in video surveillance systems and i'm currently researching for free aplications for linux...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.