【问题标题】:Application crashes in XFCE using Mono and GTK#使用 Mono 和 GTK 在 XFCE 中应用程序崩溃#
【发布时间】:2014-05-21 05:47:50
【问题描述】:

美好的一天!

我有一个自己搞不清楚的问题,谷歌这次也没有什么合理的建议。所以这是最后的希望。

我有一个用 C# 和 WPF 编写的应用程序,我想将它移植到运行 xfce 桌面的 Debian。这是使用 MonoDevelop 和 GTK# 完成的。该应用程序非常简单 - 它连接到区域运输公司时刻表提供商​​的 API,并从附近的站点获取下一班车。

该应用程序现在在 Linux 中运行并按应有的方式更新时间表。问题是 - 它在开始 10-60 分钟后随机崩溃。没有错误或异常消息。我在日志中找不到任何东西。它只是退出(终止),就是这样。

我有一种预感,这可能与线程或缺少线程有关。下面是 MainWindow 方法的主要代码片段。也许社区可以帮助我并发现我是否做错了什么。

谢谢。

private static System.Timers.Timer timer;

public MainWindow (): base (Gtk.WindowType.Toplevel)
{
    Build ();

    uiCustomizations();

    updateTimetable();

    timer = new System.Timers.Timer(180000); 
    timer.Elapsed += new System.Timers.ElapsedEventHandler(updateTimetable);
    timer.Enabled = true;
}

private void updateTimetable (object source = null, ElapsedEventArgs e = null)
{
    try {
        Log.myLog.AddEntry("Downloading timetable");

        ReittiopasConnector ro = new ReittiopasConnector (Parameters.RequestType, Parameters.Stop, Parameters.CurrentTime, Parameters.TimeLimit, Parameters.DepLimit);
        Parser data = new Parser (ro.GetXmlStream ());


        BusDepartures departures132 = new BusDepartures ();
        data.GetBusSchedule (Parameters.Lines [0]);
        departures132.SetDepatures = data.ReturnDepartureTimes ();
        label_Left_Time1.Text = departures132.Departure;
        label_Left_Time2.Text = departures132.FollowingDeparture;

        BusDepartures departures503 = new BusDepartures ();
        data.GetBusSchedule (Parameters.Lines [1]);
        departures503.SetDepatures = data.ReturnDepartureTimes ();
        label_Right_Time1.Text = departures503.Departure;
        label_Right_Time2.Text = departures503.FollowingDeparture;
        //Bus503.DataContext = departures503;
        //}));

    } catch (Exception ex) {
        Log.myLog.AddEntry("Exception: " + ex.Message);
    }
}

【问题讨论】:

标签: c# mono debian gtk# xfce


【解决方案1】:

您需要在同一个线程(主线程,也称为 UI 线程)中调用 GTK/GDK API。但是,从代码看来,您正在使用 Timer,这意味着您可能正在从计时器启动的衍生线程中触摸 UI。

因此,您需要使用(例如)包装对 GTK/GDK 的调用:

    Gtk.Application.Invoke (() => {
          label.Text = "Done";
    });

更多信息here。

【讨论】:

  • 这正是我要找的! :) 非常感谢您的回答,现在完美运行。
猜你喜欢
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
  • 2017-11-10
相关资源
最近更新 更多