- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个应用程序来打印从服务器收到的消息。我在不同的线程中分离监听函数:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
char *read;
do {
chk = (int) recv(connfd, read, 1, 0);
if(chk==-1) {
printf("Error in recv(): connfd = %d\n",connfd );
perror(err);
}
else if (chk ==0) {
[messagesView insertText:@"\nConnection closed by remote host\n"];
printf("Connection closed by remote host\n");
}
else {
if( *read == '\n') {
[messagesView insertText:@"\\n\n"];
printf("\\n");
}
else if (*read == '\r') {
[messagesView insertText:@"\\r\r"];
printf("\\r");
}
else {
[messagesView insertText:[NSString stringWithFormat:@"%c",*read]];
printf("%c", *read);
}
printf(" -- %d\n",*read);
}
} while (chk>0);
[pool drain];
chk 和 connfd 是 int,messagesView 是 NSTextView*。当我调用 [messagesView insertText:]
时,应用程序崩溃,并且在标题中收到错误。如果我评论所有这些调用,则应用程序可以正常工作,并且我可以在控制台中读取正确的消息。有什么建议吗?
最佳答案
辅助线程实际上不应该接触 GUI。您需要将信息传递回主线程上的对象并更新 TextView 。
来自Threading Programming Guide :
Threads and Your User Interface
If your application has a graphical user interface, it is recommended that you receive user-related events and initiate interface updates from your application’s main thread. This approach helps avoid synchronization issues associated with handling user events and drawing window content. Some frameworks, such as Cocoa, generally require this behavior, but even for those that do not, keeping this behavior on the main thread has the advantage of simplifying the logic for managing your user interface.
关于cocoa - EXC_BAD_指令: task_thread failed cocoa,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5704711/
我是一名优秀的程序员,十分优秀!