0

My Executable file (C# WPF Solution) is not running properly when being called by MSMQ Trigger.

I am sending a notification mesasge to the queue (named sometestname): I have a class called MyNotification that includes this static method:

   public class MyNotification {
    public static void SendNotification(String destination,String msg){
    string s = @".\Private$\sometestname" + "-" + destination;
    if(!MessageQueue.Exists(s))
    MessageQueue.Create(s,false);
    MessageQueue q = new MessageQueue(s);
    Message msg = new Message();
    msg.Body = msg;
    q.Send(message);}
    }

a WPF Solution that uses this class:

void ButtonBase_OnClick(object sender,RoutedEventArgs s){ MyNotification.SendNotification("user1","testing message");}

the .exe that will be fired from trigger (when the message is received) :

  Public MainWindow(){
     String queueName = @".\Private\sometestname-user1";
    MessageQueue msgq = new MessageQueue(queueName,false);
    try{
    msgq.Purge();
    }
    catch(MessageQueueException e){
    string filename = @"C:\Users\user1\Desktop\error.txt";
    File.Create(filename);
    }
}

Note that:the message is arriving to the queue,and the executable file is showed running in the background,but it catches an error. When the executable file is executed manually,it executes nromally without any errors(i.e. no file is created). But When its fired from the MSMQ Trigger(Includes a rule the invoke standalone executable when message arrives),it catches an exception(i.e. file is created).

Galilo Galilo
  • 519
  • 5
  • 22
  • 1
    Most likely, the app is being run in a non-interactive mode, and thus there can be no UI, thus no message pump or any other GUI elements to recieve windows messages (such as button clicks, much less creating windows). – Erik Funkenbusch Apr 10 '19 at 12:42
  • and how can it be fixed? – Galilo Galilo Apr 10 '19 at 12:46
  • Also make sure that the application that has to be executed from the trigger has enough rights for the user: NETWORKSERVICE – Leon Apr 10 '19 at 12:47
  • the task manager is showing that the process is executing but under SYSTEM Username – Galilo Galilo Apr 10 '19 at 12:48
  • 1
    @GaliloGalilo - There really isn't much you can do. The problem is that a GUI app needs a Destktop Window Station to display the UI on, and Microsoft has long since deprecated allowing apps to interact with the desktop like that. Your only option is to rewrite the app as a console application. – Erik Funkenbusch Apr 10 '19 at 13:20

1 Answers1

1

I think you could create a windows service that runs your exe. Instead of having a MSMQ trigger, the service would listen to msmq for a message to appear.

A windows service still can't just run a desktop app. At least not in a straight forward way.

It can use the approach explained here:

https://www.codeproject.com/Articles/35773/Subverting-Vista-UAC-in-Both-and-bit-Archite

There's a comment in this thread from someone says they got the approach to work on Win10.

How can I run an EXE program from a Windows Service using C#?

Having said that.

You will need to write, install and run that windows service.

Maybe a simpler approach would be to start up your wpf app minimised and with it's taskbar icon hidden when the user logs on. That then listens for the msmq message. When it receives one it shows a window.

Andy
  • 11,864
  • 2
  • 17
  • 20