Background task for simple operations? - vb.net

I need to play several sounds, and if one sound is already playing, the subsequent sounds will be queued.
I'm using MCI API calls.
Until now, I'm using this sound player as a COM control with InterOp.
However, the COM control causes some COM Release errors, so I would like to solve it differently.
I would like to know if I should solve this by a usual NET class in my form using a background task so that I'm completely indepenent on how heavy the load of my application is so that finished sounds can immediately trigger playing the next sound without any delay, or if that would be an overkill, and background tasks should only be used on long, blocking operations.
My sound player basically only uses a timer to check with an API call if the MCI has stopped playing and then starts the next sound.
Also, it provides events when a sound was started, stopped or errored-out.
Or if I should encapsulate the player in a separate NET project and reference it? Then I would also be independent on the workload of the main application.
Seamless playing of the sound queue would be essential to me.

Related

How to continuously get updates for AudioQueue without a timer

I know you can use the AudioQueueGetCurrentTime method to get the current time. Is there a callback or something that continuously gets called to update your timeline? Or do you just have to create a timer to continuously call AudioQueueGetCurrentTime?
I don't remember any other way than timer, but I guess it is possible to implement periodic notifications without timer since you manually enqueue buffers and you know bitrate, etc...
As a side note: why not give a try to AVFoundation (available since iOS 4), it's all implemented there and until you have standard formats playback or you have to support iOS 3.x you don't really have to deal with AudioQueue horror.

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.

Techniques for keeping iOS NSTimers from being killed in background

Relatively new to obj-c and iOS but I've created a little app that is built around 4 simple, stopwatch-style timers. The user starts up a timer, it starts counting and they go on with their life. When they open up the app they can see how long it's been going. Individually, these timers would be identical to the one in the Apple Clock app.
This works "most" of the time. I've had timers running for days, started, stopped, reset, started up again. What I've noticed though is that if the app is pushed just a little too far down the multitasking drawer, the next time I open up the app all my timers will be at zero, and the app will be in it's freshly launched state.
To me this appears related to the OS thinking my app is not needed, killing its threads/processes/whatever to free up memory. For the apps intended audience, it will be a frequently checked app so this may not crop up as a problem, but it seems that there should be some technical approach to ensure that my stopwatches never fail. I'm just not sure where to look for this kind of functionality. Any thoughts appreciated.
Thanks!
You are doing it wrong.
If you build a stopwatch app with 10 timers you need exactly one NSTimer.
And this timer refreshes the display. This timer is not needed when the app is in background.
NSTimers have the problem that they can be late, and they should not be used to schedule time critical stuff (like counting a second up).
Store the current NSDate when you start a timer and display the difference in the app.
Store those NSDates to NSUserDefaults and they will even survive a restart of the device.
Try this approach: For your "timers" keep track of the start time only, and for display purposes calculate the number of seconds since the start time and display accordingly.
Store these start times so that even if the app is "evicted", you'll still know when a timer was started and can calculate how much time has elapsed.
The only reason to use an NSTimer would be if you want to show the timer's seconds ticking by.
Also have a look at the delegate methods applicationDidEnterBackground and applicationWillEnterForeground. In these methods you'd want to invalidate and re-establish an NSTimer (if you're using one), respectively.
There is a lot of Apple documentation about what you can and cannot do in the background here.
Unfortunately there is no way to ensure that your app will continue to run in the background the way you want it to.
You can use -beginBackgroundTaskWithExpirationHandler: to request up to ten minutes of additional execution time but in all likelihood your application will be terminated.
I've heard an Apple engineer refer to this as 'evicting'.

Form transitions in VB.NET without using Timers

For couple of hobby projects of mine, I've been performing form transitions (fade-in/out, slide-left/right) using timer control, and I know its not the right way to do it. Using timer has its own disadvantages as it is a CPU hog if logic is complex and also, transitions are not smooth. So, I'm wondering how can I perform form transitions without using any timers and just by using native Windows API or any third-party library. I came across with FlowFX but found that it is limited only to .NET Compact framework.
Thanks...
We don't know what your timer handler is doing without a code sample, but using a timer to do animations is an acceptable method.
Here is another SO question and answer that might show you a better way of coding your timer handler.
The "not smooth" part could perhaps be overcome using double-buffering.

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.