Windows service onstop will kill the thread? - multithreading

I have a windows service where I create a thread for doing the background process. So once the thread finishes the task, do I need to call the service ‘stop’ event to make the service stop? (like this.stop() in code)
I didn’t clearly understand why it’s asking to not write code in onstart event and asked to create a thread. Is it because the service will stop after a particular time? Will it cause my thread also to stop, when the service ends? Because I don’t need that. The thread should handle a long running process; I need the thread not killed until it finishes the task.

Related

Process Service Thread

We know about relation between Process and Thread.
Thread comes under Process, we can say Process is a container and Thread is an element of a container.
But what about Service ?
I can say Process and Thread having same genre.
Can we say the same thing for Services?
I found Window Services and Android Services having similarity, say in Android if we want to play Media then we have to get getSystemService(Context.AUDIO_SERVICE) likewise in Windows (8) if you stop Windows Audio (audiosrv.dll) services from services.msc then Media will not play.
What is Service?
Windows
A service is an application type that runs in the system background without a user interface and is similar to a UNIX daemon process.
Android A service is a component which runs in the background, without direct interaction with the user.
A service runs by default in the same process in the main thread as the application.
Services which run in the process of the application are sometimes called local services.
With above definition we can say apparently that Service is also a Process (i am not sure, please make me correct)
Let me start with the statement - Service is not a process. It is an activity without GUI
If you start a thread, it runs parallel with your main activity thread. But a Service is not guaranteed to always run in a new thread. So, you cannot call a service similar to Thread.
A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
So When to use of a service in your application ?
If your application tells the system about something it wants to be doing in the background (even when the user is not directly interacting with the application). This corresponds to calls to Context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it.
Reason - Your application together with all its global variables will not be wiped out as long as there is a Service still running. So if the user is not interacting with your application and some other application in foreground needs more memory and if the OS triggers a low memory warning and your activity is destroyed, still your application is not completely lost as the service is running.
A facility for an application to expose some of its functionality to other applications. This corresponds to calls to Context.bindService(), which allows a long-standing connection to be made to the service in order to interact with it.
How the Application priority is defined based on service ?
If the service is currently executing code in its onCreate(), onStartCommand(), or onDestroy() methods, then the hosting process will be a foreground process to ensure this code can execute without being killed.
If the service has been started, then its hosting process is considered to be less important than any processes that are currently visible to the user on-screen, but more important than any process not visible.
If there are clients bound to the service, then the service's hosting process is never less important than the most important client.
A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory.

Difference between looper and a service

I have read that a service is different from thread because it is not affected by the current activity. But threads are. But Looper is also same like a service which doesn't get affected by the current activity and they both run in background.
So what basically differs a service and a looper... ?
Please help me out. I am totally confused in these topics.
Looper is a class which is used to execute the Messages(Runnables) in a queue. Normal threads do not have any queue. For example, Simple threads do not have any queue. They are for one time execution and after the end of the code the thread will be stopped/killed and it will not able to run another Message(Runnable).
Usage
If you wants to execute multiple messages(Runnables) then you should use the Looper class which is responsible for creating a queue in the thread. For example. If you are writing an application which downloads files from the internet then you can use Looper class to put all the files in the queue to be downloaded.
Service runs in a different process, Say in an application if it gets crashed or the process in which application was running gets killed then Async Task/looper will also be killed but not Service because service was running in a differnt process. Looper run in the same thread as of app unkike Service .Also Android Recommends to use Async task rather than Loopers.

Scheduling the created thread immediately

i am using PsCreateSystemThread() for creating a thread. But it is not getting scheduled immediately. How can i force that thread schedule immediately after the creation of the thread.
Scheduling can be forced by KeSetPriorityThread and/or KeSetBasePriorityThread.

Linux service and Source for cron job

I am new to linux and writing a service in C++ which spawns multiple threads and I am starting the service by calling it from init.d, but how should I send the terminate signal to my application from the script , so that my service terminates all the threads and exits.
And also where can I find the source code for any linux services. e.g. /etc.init.d/rc5.d/S14cron . It will be helpful in understanding how to implement a service.
The classic reference for this kind of question is Steven's "Advanced Programming in the UNIX Environment". You can find the source code to this text book here.
Depends what your application does.
Personally I'd keep a thread just for handling signals and call sigprocmask in the other threads to stop signals being delivered to them.
The main thread / signal handling thread (it is usually a good idea to make this the main thread) can then send a message to its threads to tell them to finish what they're doing and quit.
Alternatively, if you like the principle of crash-only, you could just call exit_group and be done with it :)

How can you detect if your MFC application is not responding?

How can you detect if your MFC application is not responding?
Either the same application can start a separate thread, or some other application can run its own thread and periodically call SendMessageTimeout() to send the WM_NULL message to the application in question. If it times out it means that the application is irresponsive.
If you're asking how to do it from within the process itself, you can't, it's a paradox. A blocked process can't detect if it is not responding. It'd be like someone waking himself up to ask himself if he's sleeping.
Based on this and your other question, I'd guess you have a long-running operation and you want the user to wait until it's finished. If they click your window before it's done they get "not responding" and may terminate the application too early.
You need to perform the long-running operation on a separate thread. Here's a great starting point: CodeProject article

Resources