Search code examples
bashraspberry-pimonoraspbianrc

Mono WinFroms app won't run on boot


I am new to Raspbian. I have a mono WinForms app on Raspbian on RPi + official 7" display. It works fine when I execute start-app.sh script via the UI (double click the file) and via the terminal (/home/pi/scannerapp/start-app.sh).

Script (start-app.sh):

#!/bin/bash
/usr/bin/mono '/home/pi/scannerapp/Re-Express Scanner WinForms.exe'

I edited the rc.local file with:

exec 2> /tmp/rc.local.log  # send stderr from rc.local to a log file
exec 1>&2                      # send stdout to the same log file
set -x                         # tell sh to display commands before execution

/home/pi/scannerapp/start-app.sh &
exit 0

And on boot, the app doesn't run. I find the following in the log:

   Unhandled Exception:
System.TypeInitializationException: The type initializer for 'System.Windows.Forms.XplatUI' threw an exception. ---> System.ArgumentNullException: Could not open display (X-Server required. Check your DISPLAY environment variable)
Parameter name: Display
  at System.Windows.Forms.XplatUIX11.SetDisplay (System.IntPtr display_handle) [0x00435] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at System.Windows.Forms.XplatUIX11..ctor () [0x00077] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at System.Windows.Forms.XplatUIX11.GetInstance () [0x0001c] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at System.Windows.Forms.XplatUI..cctor () [0x0007d] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
   --- End of inner exception stack trace ---
  at System.Windows.Forms.Application.EnableVisualStyles () [0x00006] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at Re_Express_Scanner_WinForms.Program.Main () [0x00001] in <52b5a9f59ae647c0a7c47e5736c3742e>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'System.Windows.Forms.XplatUI' threw an exception. ---> System.ArgumentNullException: Could not open display (X-Server required. Check your DISPLAY environment variable)
Parameter name: Display
  at System.Windows.Forms.XplatUIX11.SetDisplay (System.IntPtr display_handle) [0x00435] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at System.Windows.Forms.XplatUIX11..ctor () [0x00077] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at System.Windows.Forms.XplatUIX11.GetInstance () [0x0001c] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at System.Windows.Forms.XplatUI..cctor () [0x0007d] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
   --- End of inner exception stack trace ---
  at System.Windows.Forms.Application.EnableVisualStyles () [0x00006] in <d5b72d15d4f7424c8a1538e3f19ec2e3>:0 
  at Re_Express_Scanner_WinForms.Program.Main () [0x00001] in <52b5a9f59ae647c0a7c47e5736c3742e>:0 

I get the same exception if I try to run the start-app.sh script via SSH.

Any help is appreciated. Thanks!


Solution

  • I ended up removing my edits from rc.local and made the call to the start-app.sh script in /etc/xdg/lxsession/LXDE-pi/autostart by editing it with sudo nano. That is now working on boot. From my understanding, calling the script from the rc.local file was doing so outside of the X display environment, hence the X display error and why it needed to be called from within the display environment (/lxde-pi/autostart) instead.

    See this for more info on auto running GUI apps: http://www.raspberry-projects.com/pi/pi-operating-systems/raspbian/auto-running-programs-gui