在MAC OS X 开发中,如何让程序有多线程?方法1
来源:互联网 发布:淘宝宝贝图片尺寸 编辑:程序博客网 时间:2024/04/29 17:59
学习目的:NSThread的使用
功能:
1):主线程,不做任何事情,如果做事,就会先执行,然后其他事件排后执行,这样导致界面会等待主线程事件完成后,才做其他的响应事件。
(这不是我们的目的,我们的目的是:一边一个事件执行中,一边其他界面的控件能去做事,而不是界面卡住状态!)
2):辅助线程1:申明,然后初始化,然后start,三步完成,停止,可以直接设置一个中间变量来停止,然后cancel,和nil就可以了。
当然:在初始化的时候,要调用你写的函数。
H文件
<pre name="code" class="objc">#import <Cocoa/Cocoa.h>@interface EDUCATIONDocument : NSPersistentDocument{ NSThread *thread_second;}@property (assign) IBOutlet NSTextField *m_ET_input;@property (assign) IBOutlet NSTextField *m_LB_GetValue;@property (assign) IBOutlet NSTextField *m_LB_Thread1_Show;@end
M文件
//// EDUCATIONDocument.m// Learn_NSThread//// Created by DMD on 31/7/14.// Copyright (c) 2014 EDUCATION. All rights reserved.//#import "EDUCATIONDocument.h"static bool g_thread1_run_flag =false;static int g_count=0;@implementation EDUCATIONDocument@synthesize m_LB_Thread1_Show;@synthesize m_LB_GetValue;@synthesize m_ET_input;- (id)init{ self = [super init]; if (self) { // Add your subclass-specific initialization here. } return self;}- (NSString *)windowNibName{ // Override returning the nib file name of the document // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead. return @"EDUCATIONDocument";}- (void)windowControllerDidLoadNib:(NSWindowController *)aController{ [super windowControllerDidLoadNib:aController]; // Add any code here that needs to be executed once the windowController has loaded the document's window.}+ (BOOL)autosavesInPlace{ return YES;}- (IBAction)OnBT_Start_Thread1:(id)sender{ [self do_thread1];}- (IBAction)OnBT_Stop_Thread1:(id)sender{ g_thread1_run_flag = false; [thread_second cancel]; thread_second = nil;}- (IBAction)OnBT_Now_Station:(id)sender{ m_LB_GetValue.stringValue = [NSString stringWithFormat:@"g_thread1_run_flag = %d,g_ount = %d",g_thread1_run_flag,g_count];}- (IBAction)OnBT_GetNumber:(id)sender{ m_LB_GetValue.stringValue = m_ET_input.stringValue;}// Show Top 1 ++-(void)closeModalWindow:(id)sender{ [[NSApplication sharedApplication] stopModal];}- (void)windowWillClose:(NSNotification *)notification{ [self performSelectorOnMainThread:@selector(closeModalWindow:) withObject:nil waitUntilDone:NO];}// Show Top 1 ---(void)do_thread1{ //主线程:不需要申明变量 [self performSelectorOnMainThread:@selector(thread_function_main) withObject:self waitUntilDone:YES]; //次线程,需要申明 thread_second = [[NSThread alloc] initWithTarget:self selector:@selector(thread_function_second) object:nil]; //次线程开始 g_thread1_run_flag = true; [thread_second start]; //和次线程一起做的事情// [NSThread detachNewThreadSelector:@selector(threadFunc3) toTarget:self withObject:nil];}-(void)thread_function_main{}-(void)thread_function_second{ if(false == g_thread1_run_flag) { return ; } int i=0; int j=0; int max =100; for (i=1; i<max; i++) { for (j=1; j<max; j++) { g_count=i*j; if (false == g_thread1_run_flag) { i=max; break; } else { [NSThread sleepForTimeInterval:0.02f];// = sleep(20 ms) self.m_LB_Thread1_Show.stringValue = [NSString stringWithFormat:@"%d x %d = %d",i,j,g_count]; } } } }//当关闭窗口的时候,执行的事件,属于协议函数 [也可以不用在H里面申明]- (BOOL)windowShouldClose:(id)window{ NSAlert* alert = [[NSAlert alloc] init]; [alert setAlertStyle:NSInformationalAlertStyle]; [alert setMessageText:@"Are you sure you want to quit?"]; [alert addButtonWithTitle:@"Yes"]; [alert addButtonWithTitle:@"No"]; NSInteger result = [alert runModal]; if (result == NSAlertFirstButtonReturn) { [self OnBT_Stop_Thread1:nil]; [alert release]; return YES; } [alert release]; return NO;}//当关闭窗口的时候 [也可以不用在H里面申明]- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication{ return YES;}@end
运行效果
测试OK!
0 0
- 在MAC OS X 开发中,如何让程序有多线程?方法1
- 让PHP跑在Mac OS X中
- Mac OS X开发中在程序运行时判断某个API是否存在
- COCOSDX2.2如何在Mac OS X 中运行Lua (Running Lua on Mac OS X)
- 如何在Mac OS X 中运行Lua (Running Lua on Mac OS X)
- 如何在Mac OS X 中运行Lua (Running Lua on Mac OS X)
- 如何在Mac OS X中安装Tomcat
- 如何在Mac OS X中安装Tomcat
- 如何在Mac OS X中安装Tomcat
- mac os x如何在eclipse中阅读jdk源代码
- 如何在Mac OS X (Lion)中安装Subversion Server
- mac os x如何在eclipse中阅读jdk源代码
- mac os x如何在eclipse中阅读jdk源代码
- 如何在Mac OS X中创建Ubuntu USB启动盘
- Unity3D在Mac OS X中如何打开两个窗口
- 如何在Mac OS X中安装Tomcat
- 在mac OS X中配置python mysql开发环境
- 在Mac OS X Lion下让Eclipse支持全屏模式的方法
- 并查集 A Bug's Life
- 不可摸数 【杭电-HDOJ-1999】 附题
- 从头开始写项目Makefile(十):make内嵌函数及make命令显示
- BeautifulSoup解析xml文件的使用初步
- linux
- 在MAC OS X 开发中,如何让程序有多线程?方法1
- 模板设计策略
- Eclipse各种小图标的含义
- Android类库打包 (续)
- poj2891 Strange Way to Express Integers
- String,StringBuffer与StringBuilder的区别
- 【最小生成树之prim算法】POJ 1789---Truck History
- poj 3669 bfs
- oracle-issue