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.
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
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.
@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?
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.
@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: - 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?
Here you go. Sorry for the delay as I was away. - //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
-
);
-
*/
-
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: - 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 - #include <boost/chrono.hpp>
-
Then I get an error "cannot find include file <boost/chrono.hpp>
-
".
Where am I going wrong?
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:
The path to the file would be a defined path in your compiler.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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;...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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???...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |