首页 > 代码库 > MAC COCOA一个简单的多线程程序[2]

MAC COCOA一个简单的多线程程序[2]

 

MAC COCOA一个简单的多线程程序[2]

使用RUNLOOP计数,实现一个时间计数器和事件at the same time 运行。
STEP 1
H
CODE:
//
//  EDUAppDelegate.h
//  test_runloop_multithread
//
//  Created by DMD on 23/6/14.
//  Copyright (c) 2014 EDU. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface EDUAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (assign) IBOutlet NSTextField *m_Label_TotalCount;
@property (assign) IBOutlet NSTextField *m_Label_EveryStartCount;

@property (assign) IBOutlet NSTextField *m_Edit_SetCurrentNumber;

@property (assign) IBOutlet NSTextField *m_Label_EveryStartCount_Set;

@property (assign) IBOutlet NSTextField *m_Label_Restart;

@property  CFRunLoopSourceRef m_source1;
@property  CFRunLoopSourceContext m_source1_context;
@property  CFRunLoopTimerRef m_timer1;
@property  CFRunLoopTimerContext m_timer1_context;

@property NSString *m_total_str;
@property int m_total_count;

-(void)Show_Msg;

-(void) set_label_value:(int)value;

@end

M
CODE:
//
//  EDUAppDelegate.m
//  test_runloop_multithread
//
//  Created by DMD on 23/6/14.
//  Copyright (c) 2014 EDU. All rights reserved.
//

/****************************************
 Function : 1个线程来计数,另一个线程来接受用户输入的字符串,互相之间不干扰
 目的:测试多线程程序工作,了解原理
 *****************************************/

#import "EDUAppDelegate.h"

//Define Public Variable +++++
static int g_total_count=0;
static EDUAppDelegate *m_window_main;
//---------------------------

@implementation EDUAppDelegate

//define var +++
@synthesize m_Edit_SetCurrentNumber;
@synthesize m_Label_EveryStartCount;
@synthesize m_Label_EveryStartCount_Set;
@synthesize m_Label_TotalCount;
@synthesize m_Label_Restart;

@synthesize m_source1;
@synthesize m_source1_context;
@synthesize m_timer1;
@synthesize m_timer1_context;
@synthesize m_total_count;
@synthesize m_total_str;

//---------------
- (void)dealloc
{
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    m_window_main = self;
    m_total_count=0;
    m_total_str=@"";
}

-(void) set_label_value:(int)value
{
    NSString *myname;
    myname = [NSString stringWithFormat:@"Every Start Count:%d",value];
    
    m_total_str = [m_total_str stringByAppendingString:myname];
    m_total_str = [m_total_str stringByAppendingString:@"\n"];
    
    m_Label_EveryStartCount.stringValue = http://www.mamicode.com/m_total_str;>

运行效果图:


成功!
需要补充:在定义自身类的时候,一定要初始化 =self,这样才可以使用本身类里面的成员函数或者变量
完成!