I need to create a task that runs in the background and does some operation that may take up to a few minutes (and not related to the UI).
I understand that if i create a regular task, the android OS can destroy it after some time, and the solution is to create a background service.
my question is if the line bellow can replace the service, and the task will not be destroyed by android OS until completion of the task:
Task.Factory.StartNew(() => MyTask(), TaskCreationOptions.LongRunning);
my question is if the line bellow can replace the service
Yes, you can use long time running task to handle time-consuming operation. Because their essence is the same. IntentService is equivalent of starting a new task at service. You can refer to the IntentService document.If you do not want to start a task in service please use Thread or Task and start it at any place you want.
the task will not be destroyed by android OS until completion of the task
Both IntentService and Task will not be destroyed by android OS until you killed your application.
Related
I am trying to understand how I shall port my Java chess engine to dart.
So I have understood that I should use Isolates and/or Futures to run my engine in parallell with the GUI but how can I force the engine to terminate the search.
In java I just set some boolean that where shared between the engine thread and the gui thread.
You should send a message to the isolate, telling it to stop. You can simply do something like:
port.send('STOP');
To be clear, isolates and futures are two different things, and you use them differently.
Use an isolate when you want some code to truly run concurrently, in a separate "isolated memory heap". An isolate is like a mini program, running separately from your main program. You send isolates messages, and you can receive messages from isolates.
Use a future when you want to be notified when a value is available later. "Later" is defined as "a future tick in the event loop". Each isolate has its own event loop. It's important to understand that just asking a Future to run a function doesn't make the function run in parallel. It just puts the function onto the event loop to be run "later".
Answering the implied question 'how can I get a long running task in an isolate to cease running?' rather than more explicitly asked 'how can I cause an isolate to terminate, release it's resources and generally cease to be?'
Break the long running task up into smaller, shorter running units.
Execute each unit with a Future. Chain futures as appropriate.
Provide a flag that each unit should check before executing its logic. If the flag is set, bail.
Listen for a 'stop' message and set the flag if/when received.
Splitting the main processing task up into Futures allows processing of the stop message to get onto the event queue ahead of units of processing of the main task.
There is now iso.Isolate.kill()
WARNING: This method is experimental and not handled on every platform yet.
I am trying to understand multi-threading on iOS in more detail. I went through some of the class references like NSThread, NSRunLoop, NSTask..
First of all as indicated on the following link:
use of runloop
Runloop runs within a Thread.
So why do we need to define our own Runloop in our app? In the case of NSThread it is useful because some of time-consuming processes can run in a separate thread so that the app will still be responsive on the main thread.
Interacting with the thread's run loop may be useful if you have a thread whose work you want to continue periodically. That is, a run loop would do some work, and then when it is finished with that work, it would put the thread to rest for some time, then resume work at a later time -- effectively preventing the thread from exiting. You won't need to interact with them or configure/create them yourself regularly (only a small percentage of apps would qualify, if you are using high level abstractions such as Foundation because Foundation would set them up on your behalf in most scenarios).
If your secondary thread just does a specified task and does not need to wait for some external event (e.g. a download to finish), you would (typically) not need to interact with the run loop.
You might consider looking at using NSOperationQueues, NSOperations and NSBlockOperations instead as these will manage themselves, will allow for cancellation of tasks and can be scheduled on main and background threads.
I have got Sinatra/Rails app and an action which starts some long process.
Ordinary I make a queue for background jobs. But this case is too simple and background process starts very rarely, so queue is an overhead.
So how could I run background process without queue?
get "/build_logs/:project" do
LogBuilder.new(params[:project]).generate
"done"
end
I've tried to make it as a new Thread or Process fork, but it didn't help.
I have had success with this (simplified) in Sinatra:
get '/start_process'
##pid = Process.spawn('external_command_to_run')
end
This returns the Process ID, which you can use to terminate the process later if you need. Also, this is on Linux, it will not work on Windows.
I have an issue of Time out error when i click on the Export to Excel link in my application, due to heavy db transactions.
I want to change this functionality to the background so that user able to move forward while background process runs.
I want when user clicks on Export to Excel link background/new thread process runs and an email is sent to the user with a link to download a file.
I want to know which is best way to achieve this Creating new thread or Background process?
Thanks in Advance
You may want to use https://github.com/defunkt/resque or https://github.com/collectiveidea/delayed_job.
You can also watch:
http://railscasts.com/episodes/271-resque
http://railscasts.com/episodes/171-delayed-job-revised
http://railscasts.com/episodes/171-delayed-job (old one of above but free - but it is worth to buy a subscription)
There are three types of messages queues,
No queues - a new thread / process will be triggered for background task, eg: spawn
Database driven - corresponding task will be stored in a table in the database and will be deleted automatically on completion, eg: backgroundrb, delayed job
Message queues - Stored jobs in memory, eg: starling
I haven't used resque, but heard that it is bit complicated to configure.
Apart from running tasks in background, if you want to run some repetitive task on regular intervals, you can go for backgroundrb. Otherwise delayed job will be right choice.
I have to do 3 async operations parallely inside a Windows NT Service (using .Net2.0/C#). For that I am using Backgroundworker component.
Is it a good option/approach?
For continuous operation, I am calling RunWorkerAsync() again in RunWorkerCompleted event.
Please suggest me.
Usually BackgroundWorker is used for long-time operations. If you just need to execute 3 tasks in parallel you can use simple threads.
Usually, BackgroundWorker is used when you need RunWorkerCompleted to perform updates in a GUI. At least that's how I've been using it. It sounds like you want this to run constantly, so why not use a regular worker thread?
The biggest issue I see is that BackgroundWorker uses the .NET thread pool, so if you really want this to run continuously, you've just guaranteed to use up one of the thread pool threads, of which there are a limited number available.
I wouldn't be using backgroundworkers for the job you're describing here.
The background worker is meant to be used for some 'long' running operations while keeping a responsive UI. Using services breaks the UI pattern.
For a service I would be using a timer and three threads.
Let the timer check the existence of the three threads and restart them or report errors when needed. The three threads can do their own job in a while loop (don't forget to add a sleep(0) in there)