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

  • SEARCH
  • Home
  • 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 8852511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:25:11+00:00 2026-06-14T13:25:11+00:00

After some questions here I decided to run a server like this: @implementation Server

  • 0

After some questions here I decided to run a server like this:

@implementation Server
-(id)init
{
if (self = [super init])
{   
    shouldRun = true;
    __block Server* blocksafeSelf = self; // should prevent retain cycle

    myServer = ^() {
                    dispatch_async(dispatch_get_main_queue(), ^{
            NSString* currentText = controller.output.text;
            controller.outputTextField.text = [currentText stringByAppendingString:@"Server ready. \n"];
        });

        while (blocksafeSelf.shouldRun) {
           int bytes_received = recvfrom(...);
            if (bytes_received > 0) {
                switch (TYPE OF RECEIVED PACKET) {
                    case 1: {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            NSString* currentText = controller.outputTextField.text;
                            controller.outputTextField.text = [currentText stringByAppendingString:@"S: Received Typ 1 "];
                        });
                        [blocksafeSelf methodToDealWithPacket:(PACKETTYPE*) buffer];
                        break;
                    }
                    ...
                    default: {
                       ...
                    }
                }

            }
        }

return self;
}
@synthesize shouldRun;

the corresponding .h file:

@interface Server : NSObject {
    ServerBlock myServer;
    ....
}
@property BOOL shouldRun;
....
@end

Now I have a ViewController, which has the Server as a property and following method in its implementation:

// called when the user clicks the stop button
- (IBAction) clickedStop
{
    if(theServer != nil) {
        theServer.shouldRun = false;
    }
}

When I click the stop button however, the server does not exit his while loop. Why?

edit: Here is, how the server is started in the ViewController:

// called when the user clicks the start button
  - (IBAction) clickedStart
   {
   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

    if (...) { // all needed data has been entered
        // start server here
        if(theServer == nil) {
            theServer = [[SNPTServer alloc] initWithHost:serverIP.text AndPort:    [serverPort.text intValue] AndViewController:self];
            dispatch_async(queue, theServer.myServer);
        }
    }

}

As you see, I have altered the constructor above a bit.

  • 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-06-14T13:25:12+00:00Added an answer on June 14, 2026 at 1:25 pm

    My answer will only work if the body of your while loop looks similar to the following (I will assume it does)

    while (blocksafeSelf.shouldRun) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                 beforeDate:[NSDate distantFuture]];
    }
    

    In this case, just setting shouldRun to NO is not enough. That while loop is running on a different thread/runLoop so an event must happen to cause the run loop to pop so that the condition is checked again.

    The easiest way to do that is to set shouldRun to NO from the thread that server is running on. I would do something like this:

    myServer = ^() {
        self.runThread = [NSThread currentThread]; // runThread declared elsewhere
        while (blocksafeSelf.shouldRun) {
              ....
            }
        }
    

    And then when you want to cancel it do:

    - (IBAction) clickedStop
    {
        if ([NSThread currentThread] == theServer.runThread) {
            if(theServer != nil) {
                theServer.shouldRun = NO;
            }
        } else if (theServer.runThread) {
            [self performSelector:_cmd
                         onThread:theServer.runThread
                       withObject:nil
                    waitUntilDone:NO];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I finally decided to post a question here after some time spent trying to
I came across couple of questions about OCL expressions. After reading some university slides
After reading this question, I need to clear up some things. IQueryable<Customer> custs =
I've decided to come back to C++ after some time spent in Java and
Hey so after reading this article I've been left with a few questions I
Yesterday I asked the question on how I should save my files. After some
After some time researching and trying different things I still cannot get my @ExceptionHandler
After some desperate assistance regarding prototype.js on Magento. Please see the below product page
After some great help from Josh Mein with a Javascript Hide/Show menu, the menu
After some hours of trying, I want to ask how to loop a video

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.