How do I restart a frozen screen in Ubuntu without losing any open windows?

I had a device running Ubuntu 12.04 LTS have its screen freeze. Generally I have simply jumped onto an available tty, i.e. Ctrl-Alt-F1 and executed the command /etc/init.d/lightdm restart or service lightdm restart. This however kills everything I have open. How do I avoid this?


DISCLAIMER: According to Brenden's comment, this might be outdated and not work anymore with newer versions of Ubuntu. Also please be prepared that you might lose your open windows.

Now I got it for Unity (I just replaced my GNOME shell with unity without logging out or closing windows):

  1. Go to tty1 [Ctrl]+[Alt]+[F1]
  2. Type in DISPLAY=:0 so that programs know on what port to find the X-Server
  3. export DISPLAY
  4. unity --replace or gnome-shell --replace (you can also replace GNOME-Shell by unity or Unity by the GNOME-Shell)

This only works if not the X-Server or GNOME is frozen but only Unity or the GNOME-Shell.

How it works: The communication between programs and the X-Server for displaying their UI is working over a network socket. This normally is at "localhost:0" short ":0". By setting the DISPLAY variable, the programs know where to find the X-Server to communicate with it, so programs are displayed on the X-Server on tty7 even if the program is running on tty1.

The content of windows is managed by the X-Server whereas the position and borders of the windows is managed by the window manager integrated in unity or the GNOM-shell so there is a way to replace only them without restarting the applications on the X-Server.

PS: I'm just describing what I understand it to be, so please edit and correct me if I'm wrong in some points.

EDIT: For the GNOME-Shell, there is another way to restart it. After changing to tty simply type in pkill -HUP gnome-shell, I don't know if there's something similar for Unity.