个人学习笔记存放地

Communicating With the Target Application 进程间通信方法

上一篇 / 下一篇  2008-04-15 15:39:18 / 个人分类:MAC

先看一个例子

#include <unistd.h>
#import <Foundation/Foundation.h>

static NSString *ThreadTestDidFinishWork = @"ThreadTestDidFinishWork";
static BOOL gDone = false; // how old fashioned ...

@interface ThreadTest : NSObject
{
}
- (void)startWork;
- (void)doWork:(id)sender;
- (void)finishWorkHandler:(NSNotification*)note;
@end

@implementation ThreadTest

- (void)startWork
{
    // register our handler
    [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(finishWorkHandler:) name:ThreadTestDidFinishWork
object:nil];
   
    // start our thread
    [NSThread detachNewThreadSelector:@selector(doWork:) toTarget:self withObject:nil];

}

- (void)doWork:(id)sender
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSLog(@"work thread: %@", [NSThread currentThread]);

    // do time intensive work
    sleep(5);
   
    // post notification
    [[NSNotificationCenter defaultCenter]
postNotificationName:ThreadTestDidFinishWork object:nil];
   
    [pool release];
}

- (void)finishWorkHandler:(NSNotification*)note
{
    NSLog(@"note thread: %@", [NSThread currentThread]);

    // unregister
    [[NSNotificationCenter defaultCenter] removeObserver:self];
   
    // quit
    gDone = true;
}

@end

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    ThreadTest *test = [[ThreadTest alloc] init];

    NSLog(@"main thread: %@", [NSThread currentThread]);

    [test startWork];

    // please don't write real apps like this! this is just for the
    // sake of the test! use real run loops! I beg you!
    while (!gDone) {
        (void)[[NSRunLoop currentRunLoop]
runMode:NSDefaultRunLoopMode      beforeDate:[NSDate distantFuture]];
    }
   
    [test release];

    [pool release];
    return 0;
}


When the above program is compiled and run it yields the following output:
输出结果:

[localhost:Shared/Builds/mtrent] mike% ./threadNoteTest
Apr 25 00:57:03 threadNoteTest[384] main thread: Thread 0x4c200
Apr 25 00:57:03 threadNoteTest[384] work thread: Thread 0x5f940
Apr 25 00:57:09 threadNoteTest[384] note thread: Thread 0x5f940

So it's clear our notification was posted and received on our worker thread.

以上是线程间在同一进程通信

 

以下进程间通信:我根据以上程序修改的

接收端:

#import <Cocoa/Cocoa.h>

static NSString *ThreadTestDidFinishWork = @"ThreadTestDidFinishWork";
static BOOL gDone = false; // how old fashioned ...

@interface ThreadTest : NSObject
{
}
- (void)startWork;
- (void)doWork:(id)sender;
- (void)finishWorkHandler:(NSNotification*)note;
@end

@implementation ThreadTest

- (void)startWork
{
    // register our handler
    [[NSDistributedNotificationCenter defaultCenter] addObserver:self
            selector:@selector(finishWorkHandler:) name:ThreadTestDidFinishWork
              object: @"Pig"];
   
    // start our thread
//    [NSThread detachNewThreadSelector:@selector(doWork:) toTarget:self withObject:nil];
 
}

- (void)doWork:(id)sender
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
    NSLog(@"work thread: %@", [NSThread currentThread]);
 
    // do time intensive work
    sleep(5);
   
    // post notification
//    [[NSNotificationCenter defaultCenter]
//postNotificationName:ThreadTestDidFinishWork object:@"test"];
   
    [pool release];
}

- (void)finishWorkHandler:(NSNotification*)note
{
//    NSLog(@"note thread: %@", [NSThread currentThread]);
    NSLog(@"NSNotificationCenter name: %@", [note name]);
    NSLog(@"NSNotificationCenter object: %@", [note object]);
    NSLog(@"NSNotificationCenter Info: %@", [[note userInfo] objectForKey: @"test"] );
 
    // unregister
    [[NSNotificationCenter defaultCenter] removeObserver:self];
   
    // quit
    gDone = true;
}

@end

int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
    ThreadTest *test = [[ThreadTest alloc] init];
 
    NSLog(@"main thread: %@", [NSThread currentThread]);
 
    [test startWork];
 
    // please don't write real apps like this! this is just for the
    // sake of the test! use real run loops! I beg you!
//    while (!gDone) {
//        (void)[[NSRunLoop currentRunLoop]
//runMode:NSDefaultRunLoopMode      beforeDate:[NSDate distantFuture]];
//    }
   
//    [test release];
 
    [pool release];
//    return 0;
    return NSApplicationMain(argc,  (const char **) argv);
}

调用段:


#include <unistd.h>
#import <Foundation/Foundation.h>

static NSString *ThreadTestDidFinishWork = @"ThreadTestDidFinishWork";

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
    NSLog(@"main thread: %@", [NSThread currentThread]);
 
 NSMutableDictionary* info = [NSMutableDictionary dictionary];
 [info setObject: @"sdfsdfdsgsdg" forKey: @"test"];
 
    [[NSDistributedNotificationCenter defaultCenter]
  postNotificationName:ThreadTestDidFinishWork object: @"Pig" userInfo: info deliverImmediately: YES];
 
 [info release];
    [pool release];
    return 0;
}

本人经验有限,希望大家多提意见


TAG: Application With Target 通信 进程

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

我的栏目

日历

« 2008-08-25  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 255
  • 日志数: 17
  • 图片数: 1
  • 文件数: 4
  • 建立时间: 2008-01-26
  • 更新时间: 2008-07-09

RSS订阅

Open Toolbar