Wednesday, February 13, 2013

Running a thread in Background in iPhone

Hello friends , welcome to my post on  Multithreading in iPhone


Small Intro to Threads
Process means a program in execution and Thread is the basic unit of processing .
Especially threads are used , when you want to run big  code that takes much time in background without creating any hinderance in
the execution of our application Thread can be created , sleeped , resumed , terminated.
You can create many threads in our application as per your requirement , but you should efficient code to manage those threads .
If not managed properly , you app may crash

About my App

In this app , i have demonstrated how you can do the following things


1)run a thread in background
2)use NSTimer in conjunction with NSThread
3)sleeping of one thread does not affect other thread

My Code :-
I will create one extra thread other than main thread .
I have made two labels and two buttons , one for each thread .



On clickMethod of main thread ,buttons changes it titleLabel value and depending on it ,
it goes into sleep state and resumes back . Same is the case with NEWTHREAD
The new thread in run into background




To see the execution of each thread , I have made to selector functions which starts timer for both the thread which can go into sleep state , also it changes an integer value of each thread .It logs the current value integer variables
as follows. This shows  the  state of thread


Finally , we can now run the app , we see that concurrent execution of threads as follows

Thanks !!