回调函数循环时占用线程问题,求助!!!!!
本帖最后由 xzx951753 于 2015-1-19 16:01 编辑哪位高手帮帮忙,我写了个回调函数,他有一个循环,我代码里面是循环体每执行一次往text_buffer中写一次,但很奇怪的是,每次点击按钮后,都要等循环完了才会显示到text_buffer中,这是为什么啊?要怎么修改才能每循环一次都往text_buffer中写一次呢?
#include <gtk/gtk.h>
#include <winsock.h>
typedef struct _options{
GtkTextBuffer *buffer;
GtkWidget *pbar;
}options;
void button_event(GtkWidget *widget,options *pdata){
const gchar *text = "How are you!\n";
GtkTextIter start,end;
gdouble new_val;
for (int count = 0 ; count <= 10000 ; count++){
gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pdata->buffer),&start,&end);
gtk_text_buffer_insert(GTK_TEXT_BUFFER(pdata->buffer),&end,text,strlen(text));
new_val = gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(pdata->pbar))+0.01;
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pdata->pbar),new_val);
Sleep(1);
}
}
int main(int argc,char *argv[])
{
options *pdate;
GtkWidget *window; //窗体
GtkWidget *scrolled; //滚动窗
GtkWidget *text; //文本框
GtkWidget *hbox;
GtkWidget *button;
/*设置主窗口*/
gtk_init(&argc,&argv);
hbox = gtk_hbox_new(FALSE,0);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window,200,200);
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
/*创建滚动窗*/
scrolled=gtk_scrolled_window_new(NULL,NULL); /*创建滚动窗口构件*/
gtk_box_pack_start(GTK_BOX(hbox),scrolled,FALSE,FALSE,10); /*把滚动闯放入盒子*/
/*创建视图缓冲区*/
pdate->buffer = gtk_text_buffer_new(NULL);
/*创建文本视图构件*/
text=gtk_text_view_new_with_buffer(pdate->buffer);
gtk_container_add(GTK_CONTAINER(scrolled),text);/*将视图窗放入滚动窗*/
/*创建按钮*/
button = gtk_button_new();
gtk_box_pack_start(GTK_BOX(hbox),button,FALSE,FALSE,10); /*将按钮放入盒子*/
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(button_event),pdate); //设置按钮信号回调函数及参数
/*创建进度条*/
pdate->pbar = gtk_progress_bar_new();
gtk_box_pack_start(GTK_BOX(hbox),pdate->pbar,FALSE,FALSE,10);
gtk_container_add(GTK_CONTAINER(window),hbox);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
当然了,界面更新是在主循环中完成的,等你自己的循环完成后才会继续去主循环,你可以试试手动刷新。
页:
[1]