按下按钮暂停运行功能

时间:2016-04-04 09:40:50

标签: c function while-loop gtk2

我有一个gtk2 +代码,其中有一个运行按钮和一个停止按钮。我希望停止按钮暂停RUN功能(不要离开它)到特定点,直到再次按下运行按钮,继续在同一点。

问题在于while循环阻止了与程序接口的任何交互并冻结。

void STOP(GtkWidget *widget, GObject *context_object_stop) 
{
  stop=1; 
}

void RUN(GtkWidget *widget, GObject *context_object_run) 
{

  GtkEntry *buffer= g_object_get_data (context_object_run, "buffer");
  GtkTextIter iter; 
  GtkTextMark *marker; 
  marker = gtk_text_buffer_get_insert(buffer);
  gtk_text_buffer_get_iter_at_mark(buffer, &iter, marker);

  stop=0;
  int i=0;

    for (i=0; i<5000000; i=i+1)
    {
     while (stop==1)
     {
       //here is my problem
     }
     gchar * stuff = g_strdup_printf("%d""\n", i);
     gtk_text_buffer_insert(buffer, &iter, stuff, -1);
     g_free(stuff);
     while (gtk_events_pending())
     gtk_main_iteration();
     gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(wins), marker);
    }

}

任何接近解决方案的想法?

EDIT;一种不停止运行主函数的多线程方法:

  stop=0;
  int i=0;
  pthread_t th1;

    for (i=0; i<5000000; i=i+1)
    {
     void *StopRun(void *arg)
     {
     while (stop==1)
     {
       //here is my problem
     }
     }
     if (stop==1)
     {
     pthread_create(&th1, NULL, (void*)StopRun, NULL);
     }
     gchar * stuff = g_strdup_printf("%d""\n", i);
     gtk_text_buffer_insert(buffer, &iter, stuff, -1);
     g_free(stuff);
     while (gtk_events_pending())
     gtk_main_iteration();
     gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(wins), marker);
    }

1 个答案:

答案 0 :(得分:0)

查看gtk_events_pending的文档和示例。它说:

  

这可用于在进行一些时间密集型计算时更新UI并调用超时等。