Running Secondary Threads in Background Continuosly - objective-c

I need to create a new thread which will have infinite while loop, so that it won't block main UI thread. The newly created thread is getting killed when I send the application to background mode, But I need to run it in background. Any help?

There is a difference between a Background Thread and a Background Task in iOS, but the underlying answer remains the same; don't run a background thread unless you simply cannot avoid it. Furthermore, you can't just spin off a thread and have it run if your app wants to support backgrounding in iOS.
First, you should read the documentation. It is extensive and provides many examples.
Then, if you have any specific questions not covered by the docs (or to clarify the docs), ask 'em here!

Related

How to launch and communicate with a window-less and console-less process from a cocoa app?

I am working with the QuickTime API and need to perform a few lengthy (as in hours) operations in the background. Unfortunately, it is not multi-thread friendly, so I am falling back to perform the tasks in a separate process, so all QuickTime related calls can happen in its main thread.
After launching it, I need a way of getting feedback on its progress, since the operations can take very long.
I am unsure of how to do this, specifically:
Should the separate process be compiled as another cocoa app or a command line tool?
How to launch it from the main cocoa app?
How to periodically get an object from it to get status information?
How to determine when it finished?
How to avoid showing a window/console when called?
How to have it part of the .app bundle so that it does not appear as a separate executable to the user?
These are really 6+ questions, but they are very related and very specific, and I think anyone needing to launch external processes (instead of spawning worker threads) can benefit from their answers. Generic code examples would be very helpful.
If it is possible, then implement the functionality in a command line tool, or another form of GUI-less application. For Cocoa applications it is possible to prevent them appearing on the Dock or in the Force Quit dialog, however a command line tool is a single binary file which does that anyway, so that would probably be a better way.
In terms of launching the tool, NSTask & NSPipe are your friends in this endeavour. The tool can definitely be kept inside your main Application's bundle, inside the Resources directory or some such, and then launched when needed. You can use the pipe to communicate back and forth.
I don't have any example code to hand, and its been a long while since I've had occasion to use either of these classes so the information I can give is limited, but it should be enough to point you in the right direction.

Running long processes inside threads in wxwidgets

Can anyone please provide me with some tutorials, explaining running processes inside threads.
I mean process control, with in wxwidgets. I'm trying to implement a gui for a console application.
Some points to remember:
The main thread is special and is the only one which can support the GUI. Worker threads can however prepare bitmaps and then use AddPendingEvent or QueueEvent to tell the main thread they have data it can use.
Never free an area of memory in a different thread from the thread in which you allocated it.
wxString is not thread safe. It is risky to use it to communicate between threads.

Cocoa - Prevent System from shutting down

Hey Guys,
I've another question I couldn't find an answer on the internet. I hope you can help me.
I'm writing an Cocoa Application which runs in background and does several tasks for me. If I quit the application, I show a NSAlert. That all works great. But here comes my problem. This Alert is presented from inside the applicationShouldTerminate: Method. So if I Logout, this method is triggered and the alert is shown. But OSX quits the application and logs out without waiting for a response even if I return NSTerminateCancel.
How can I force the OS to stop logging out/shutting down from within my application, so that the user (myself :-D) can decide what to do. (It just a dialog which asks if the done work shall be saved or not.)
I hope you can help me...
Sandro
Mac OS X is based on UNIX. When a system shuts down, the SIGKILL signal is send that terminates all the processes. But before that, the SIGTERM signal is sent that informs the processes that a shutdown is going to take place.
I think the solution lies in handling of the signals, i.e. detecting and blocking them. Regarding this, you may find guidance in GNU C Library: Signal Handling: Blocking Signals.
The examples there maybe about BSD, but you may know that the OS X core is derived from BSD and therefore OS X supports it. Moreover, C is supported in Objective-C.

What can cause a UIView to be arbitrarily removed from the hierarchy?

I have an iPhone app that, for some users, sometimes behaves as if with the main UIView has been removed from the view hierarchy. It always happens coincident with a significant event in the game. Other Core Graphics-drawn UIViews that are above it in the z-order remain, but the main one (an OpenGL view) appears to be gone, leaving the background (a solid color).
The app does not crash (it keeps running, without the view), and this seems to happen very consistently for affected users. Unfortunately I am not able to reproduce it.
I suspect a memory issue -- that would be the easiest explanation -- but based on my reading it looks like didReceiveMemoryWarning only deallocs views that aren't visible, and aside from that the memory usage for my app is pretty small. Also, the "significant event" only results in OpenGL drawing and a SoundEngine call -- no view manipulation.
Anybody out there seen something like this before?
Yes, infact one of my applications very occasionally exhibits this problem and it does seem to be memory related.
I have had no success tracking it down either by debugging or analyzing the program flow. I have verified that the view in question is destroyed and not just hidden in some way.
It happens so infrequently that I haven't looked into it to deeply, but I do think it's caused by something in the OS in some way,
You can easily test low memory in the simulator to debug this problem if it is memory related.
The problem ended up being an uncaught NSException (from a third party library) thrown in the app's timer thread, which killed the timer thread but not the rest of the app. The good news is that crash reports are generated in this case, which can make tracking it down much easier if you know to look/ask for them.
As is made clear in the SDK documentation, when your app is running low on memory, views that are not in use can be collected. When it's needed again, it's re-created. This is to conserve precious iPhone resources. Your best bet is to retain the view so it can't be released.

Best way of Multithreading

I have a college assignment due quite soon, and I need to be able to call a C++ dll that takes a long time (possibly infinte, it relies on user input)to execute. Im calling this through VB. My VB GUI freezes up when this happens, and I would like to keep the GUI responsive, so that the user can stop this possibly infinte loop.
Can anyone suggest the best/fastest way of doing this?
A bit of background, the C++ is trying to keep score on a snooker table using a webcam, and while the VB scoreboard updates easily, I would like to script it so that the analysis is almost continuous, while still allowing the user to interact. Currently the project requires the user to press a button to start the shot analysis, but it would be preferable if the program scripted itself. I have only realised this problem now and the deadline is very soon.
Update: Our lecturer suggested an option to solve the problem, but it would appear that most options here and the one he suggested will not work for us as the processing time required for the webcam image capture is too great to handle due to hardware constraints. Thanks for taking the time to help, it was much appreciated!
The best way to handle threading in VB.NET is via the System.Threading namespace.
You might also look into Application.DoEvents()
Try the system.Threading as Mark said, also try looking at Background Worker Process which is a bit simpler in VB.NET. Readup here
I would definitely use the Background Worker process. You can drag it onto your form and use the DoWork sub routine to actually do the work that is freezing your GUI thread. You can also use the ReportProgress event to actually provide progress back to your form.
As for your question regarding two separate threads, If both steps take a long time to complete I would run them in the same thread one after the other.
The one thing that could bite you with using the thread is cross-threading. In the context of your problem this means not having your second thread update form controls.
A really good resource for how to implement this code along with dealing with cross-threading is this PDF.
I also should point out that if you are using .net 1.0/1.1 you can still do the multi-threading, but don't have the luxary of having a background worker control. You'd just have to create a new thread from the System.Threading Namespace.
Just as an alternative, you could have your C++ actually processing in the background all the time. When called from VB, it would just be retrieving data from it or sending it a command (start, quit, ???) all of which would return instantly.
This could also give you more reliability since C++ would never miss video frames while VB was collecting the garbage or doing the dishes or whatever VB does in the background--C++ is going to let you be closer to a real time system.
RE: the comment about how.
What I'd probably do is have my VB programs send "Messages" to the C++ (As I said). A message is just a way to think of a function/method call--but generally they return quickly.
The "Start" message would tell the C++ code to start it's thread running and return. This is a Linux C++ thread howto, I'm not sure if you need to do something different in windows (I'd hope not, but I haven't used C++ as my main dev. language in decades).
If that doesn't work, just google "C++ Threads"
Sending a "Stop" message would stop the thread (and probably free resources).
A "Get Data" call would go to the location that the C++ thread used to store data, grab it and return.
Sorry to be so general, I'm pretty heavily Java these days.
I'm surprised nobody has suggested using a BackgroundWorker yet.