Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6059977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:46:22+00:00 2026-05-23T08:46:22+00:00

I have an interface defined like this @interface PageViewController : UIImageView { … }

  • 0

I have an interface defined like this

@interface PageViewController : UIImageView {
...
}

-(void) setImageUsing:(id) obj;

@end

and implementation is like the following

-(void) setImageUsing:(id) obj
{
    ...
    [self performSelectorInBackground:@selector(changeImage) withObject:nil];
}

setImageUsing is called from other code as result of user interactions with the application.

How should I proceed to limit PageViewController to only one background job at a time?

In other words, I would like to cancel any running background job when a new call to setImageUsing is made and start a new background job after that.

EDIT/SOLUTION:

As Ishu proposed, I tried to use NSLock for synchronization purposes. The final code (that solved the issue) looks like this:

@interface PageViewController : UIImageView {
    ...
    NSLock* m_threadLock;
    NSLock* m_externalLock;

    BOOL m_cancelThread;
}

-(void) setImageUsing:(id) obj;

@end

and implementation is like the following

    -(void) setImageUsing:(id) obj
    {
        [m_externalLock lock];
        ...
        if ([m_threadLock tryLock])
        {
            // succeeded in locking thread lock. so there is no background job running.
            // unlock the lock and continue
            [m_threadLock unlock];
        }
        else
        {
            // failed to lock thread lock. so there IS a job already running.
            // ask the job to cancel
            m_cancelThread = YES;
        }

        [self performSelectorInBackground:@selector(changeImage) withObject:nil];
        [m_externalLock unlock];
    }

    -(void) changeImage
    {
        [m_threadLock lock];
        ....
        if (m_cancelThread)
        {
           // cancel 
        }
        ...
        m_cancelThread = NO;
        [m_threadLock unlock];
     }

Hope it’ll help someone else.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:46:23+00:00Added an answer on May 23, 2026 at 8:46 am

    This is not good way to terminate any background task , competing the background thread execution is good see this thread. What you need , you need to lock an thread allocation for some kind of mishappenings. See NSLocks in developer documentation and some googling gives you some info about NSLock.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have type hierarchy defined like this: interface IMyClass { } interface IBase1<T> {
I have an interface defined like so: public interface IRipper { IRipperHost Host {
I have a class called MainGame, which is defined like this in my .h:
I have a method like this: void m1(string str) and have a class like
I have two objects defined something like this (simplified for sake of the question):
I have an NSArray which holds Foo objects defined like so: @interface Foo :
I have a defined idl file, which looks like this: module Banking { typedef
I have defined the following interface in F# [<ServiceContract>] type ICarRentalService = [<OperationContract>] abstract
I have an interface that defines some methods I would like certain classes to
I have a marker interface defined as public interface IExtender<T> { } I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.