0

I have a laptop that has both HDMI and VGA connectors; my TV is connected to the HDMI port (set at a resolution of 1600x900), and my desk monitor is connected to VGA (at an old-fashioned 1280x1024). The GPU does not allow for both external ports to be used simultaneously, so I end up having to switch between the one and the other, depending on whether I want to watch my shows and movies or sit behind the computer.

So far, so good... but (me being OCD about that stuff) I want to have a different wallpaper depending on the active config (laptop + TV or laptop + VGA), set by some script...

To catch the change between setups (Intel Graphics, using one of two preset profiles) I need something that monitors the active monitors for changes.

I've found a simple solution using SystemEvents.DisplaySettingsChanged, but this only works when I run the code from the VB.Net IDE. As soon as I compile and run the exe, the event doesn't seem to get triggered anymore.

I also tried a continuous loop using the Windows.Forms.Screen.AllScreen array, but the same applies: runs like a charm from within the IDE, but when compiled, it never detects the change.

Skeleton code for the first option (run as a console app):

Imports Microsoft.Win32
Imports System.Threading

Module Module1

    Public Sub MyEH2(ByVal sender As Object, ByVal e As EventArgs)
        Console.WriteLine("Display has changed")
        ' Actual code do change wallpaper comes here, natch
    End Sub

    Sub Main()
        AddHandler SystemEvents.DisplaySettingsChanged, AddressOf MyEH2
        While 1
            Thread.Sleep(1000)
        End While
    End Sub

End Module

My question is: why does this work when started within the IDE, but not when compiled as an EXE? (In other words: why doesn't the compiled version detect the changes?)

I'm running Windows 7 Home Premium and using VB.Net 2012 (Express)

Mehdi
  • 1
  • 2
  • My psychic debugger says that you are running VS elevated. [Try this](http://stackoverflow.com/a/2818776/17034). – Hans Passant Feb 22 '15 at 16:35
  • Tried that (even tried running the exe "As administrator"... no difference. – Mehdi Feb 22 '15 at 16:49
  • If you add attribute before your Sub Main() does that fix the problem. Alternatively, try adding a reference to System.Windows.Forms and call Windows.Forms.Application.DoEvents() in your While loop. – theduck Feb 22 '15 at 20:01

1 Answers1

0

I want to say thanks to those who came up with suggestions, but I went the lazy way... I downloaded and installed AutoIt, and managed to make a reliably working exe with it in less than 10 minutes... still using a continuous loop to check for a change in display resolution, then--when it does--run a vbscript I'd already verified to work to update the wallpaper file and force refresh the desktop.

it almost looks like it's a feature-not-a-bug thing, this seemingly simple WMI query not working from a VB .Net Express executable.

Mehdi
  • 1
  • 2