简体   繁体   中英

Application crashes in XFCE using Mono and GTK#

Good day!

I have a problem that I was not able to figure out myself, and Google this time did not have any reasonable suggestions. So this is the last hope.

I have an application written in C# and WPF that I wanted to port to Debian running xfce desktop. That was done using MonoDevelop and GTK#. The application is very simple - it's connecting to the API of the regional transport company schedule provider and grabs next bus departures from the nearby stop.

The application now runs in Linux and updates the timetable as it should. The problem is - it randomly crashes after 10-60 minutes from start. There is no error or exception messages. I could not find anything in the logs. It simply quits (terminates) and that's that.

I have a hunch that this may be somehow related to threading or lack or thereof. Below is the main pieces of the code from MainWindow method. Perhaps the community can help me and spot if I am doing something terribly wrong.

Thank you.

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);
    }
}

You need to call the GTK/GDK APIs within the same thread (the main thread, also called the UI thread). However, from the code it seems that you're using a Timer, which means that you're probably touching the UI from the spawned thread that the timer has launched.

So, you need to wrap your calls to GTK/GDK with (for example):

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

More info here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM