The reference says this:
"You should call this function from the main thread of your application only."
But I would like to get the current graphics context from separate threads. What do you think? What can I do to reach this?
Edit:
Thanks guys for the answers. I don't know which is good for me at the moment, but thank you very much.
Never draw to the screen from anything other than the main thread!!! The graphics chip is single threaded, so you could cause all kinds of race conditions if you don't follow that rule.
You can draw to a background NSImage and then notify the main thread when you're finished and to update the screen.
If you want to draw from other threads, either create your own CGBitmapContext or use a CATiledLayer.
I would say restructure your logic to conform to the reference's advice.
Generally it is accurate about things you should and shouldn't do.
Why do you want to access it from different threads?
Related
A for loop let my CPU run on 100 %. The problem is I want to change some things on the view. For example I want to use a progressView, but everything on the view only changes when the loop is finished.
Is it possible to solve my problem?
And as always, sry for my bad english.
You need to use some form of concurrency to achieve this, so that your iteration and UI updating tasks can occur in parallel with each other. Apple's Concurrency Programming Guide provides an introduction to the various forms of concurrency available.
Move your for loop onto an asynchronous GCD queue, being sure to put any UIKit messages on the main queue. This will free up the UI thread for any view changes that you want to occur.
Normally, I would just use NSOpenGLContext, but in this case I have to have a CGContextRef as the OpenGL context in order to support some other frameworks I am using. My question is: can this be done?
I cannot afford to use glReadPixels() to fill the context because it is way to slow for this. I have to render on the entire screen (1440 x 900) at least 32 times per second, so the performance is a very big concern for me. Is there a way to make OpenGL draw into the CGContextRef efficiently, or do I need some kind of workaround?
Since it seems 1 functionality will cause issues with using the GLContext and using glReadPixels() instead of a CGContext. Why not make multiple contexts?
Read here about the shared context, dedicate a Context to a background thread or operation queue and read your heavy operation into that and access it via the shared context to render it on the main thread context.
If you still must use a CGContextRef, can you tell us the exact frameworks that you are using that require it? People on here may be able to offer another solution
I have a UIScrollView in which I am placing a bunch of views as you are scrolling. it is downloading images from an api end point. The issue is that scrolling doesn't feel as smooth as it should be. I know this is because mostly something is blocking the main thread. I have searched through the whole code and commented out any UI update code, but scrolling fast is still laggy.
Any pointers on what is the best way to find which part of your code blocks the main thread?
Instruments. There's abso-freaking-lutely nothing like it. Even apple's documentation on it mentions finding high-CPU related tasks.
Personally though, I recommend the Time Profiler. Of which, an excellent tutorial may be found here.
I have a problem. Animations (switching between viewControllers, scrolling tableView) on device are working with spurts but on simulator work fine. How to solve this problem?
I'm afraid there is no easy answer.
The only solution is to improve your code and make it lighter and more efficient.
I ran into such problems too and I had no other option but to make my module work better.
The first suggestion would be to check for memory leaks (this one helped me: http://www.friday.com/bbum/2010/10/17/when-is-a-leak-not-a-leak-using-heapshot-analysis-to-find-undesirable-memory-growth/ ). Fixing the leaks should give a little more memory to work with.
If you have this spurts on the first gen ipad... meh, it's so and so. but if you have them on the ipad2, then you really need to rewrite your code: use less elements, make less operations, maybe use some background operations to perform some threads so you can leave the main thread free to make the animations
Scrolling tableViews shouldn't be too big a problem since it has been quite efficiently designed. Did you implement the method heightForRowAtIndexPat: ? If so, and you have much data in your tableView, it gets extremely slow. In that case better use a fixed height or the table.
I have a legacy WinForms Mdi App in VB.Net 2.0 which I am adding functionality to. One of the additions is a warning which needs to be raised when the current time nears a specified value (a deadline). My intention is to check the time once an hour until there is less than an hour until the deadline, then display warnings at specified intervals until the time's up.
The user needs to be able to continue to use the app up to and even after the deadline, but they need to periodically be made aware of the deadline's proximity.
The app does not use System.Threading yet and my knowledge of it is limited at this time. I do know that there are 3 different Timer() methods available:
System.Threading.Timer(),
Windows.Forms.Timer() and
System.Timers.Timer()
My question is, which is the best way to go with this? I attempted to use the threaded timer, but since WinForms are not thread safe I got a run time error trying to access another class. Is it worth making the class/form thread safe? Am I completely off track?
Thanks.
This article explains pretty well:
Comparing the Timer Classes in the .NET Framework Class Library
It sounds like System.Windows.Forms.Timer is the one for you.
My guideline: If you want the timer to run on your main GUI thread, stick with Windows.Forms.Timer. If it's okay for your timer to be called asynchronously on a thread pool thread, or if you don't want to experience the small delays that System.Windows.Forms.Timer tends to suffer, use System.Timers.Timer. System.Threading.Timer has a different interface from the other two and is not thread-safe; personally, I'm not a fan.
I would just use the Forms timer. I think I read that it's not as accurate, but it sounds like you don't need it to be.
I agree that Windows.Forms.Timer() is the best for this case as it handles the cross thread marshalling issues.
Some useful related links:
Windows Presentation Foundation Threading Model
WinForms UI Thread Invokes: An In-Depth Review of Invoke/BeginInvoke/InvokeRequred
The System.Forms.Timer actually works on the main thread using the windows message queue. This makes it somewhat inacurate but since you don't really need ms precision it's good enough.
You could use one of the other timers that work on a separate thread but since you need to activate a winforms component that work in the main you'll need to use Form.Invoke or some other way to pass the event to the main thread - which would cause some latency as well.
In conclusion use the System.Forms.Timer when you need to activate a winforms based component.
Ok, first things first...
If you want to show the user a form and do something in the background I would use the BackgroundWorker class, it worked for me before.
Also, you need invoke methods as mentioned before and as Chris said, it sounds harder than what it actually is.
Here's a link which I think will help you out.
http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx