0
Imports System.Messaging
Imports System.Collections
Imports MSMQ
Imports System.IO
Imports System
Imports System.Messaging.MessageQueue
Imports System.Runtime.InteropServices

Public Class PauseOutMessages
    'Declare everything to be in the scope of all methods.
    Dim mgmt As New MSMQManagement
    Dim outqmgmt As MSMQOutgoingQueueManagement
    Dim q As New MSMQApplication
    Dim outgoingQueues As New ArrayList
    Dim myQueue As New MessageQueue("FormatName:DIRECT=OS:myMachine\Private$\myQueue", QueueAccessMode.ReceiveAndAdmin)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        For Each queue In q.ActiveQueues
            If queue.IndexOf("DIRECT=") >= 0 Then
                outgoingQueues.Add(queue)
            End If
        Next

    End Sub

    Private Sub Pause_Click(sender As Object, e As EventArgs) Handles Pause.Click
        For Each queuePath In outgoingQueues
            mgmt.Init(FormatName:=queuePath)
            outqmgmt = mgmt.Pause()
        Next
    End Sub

    Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
        For Each queuePath In outgoingQueues
            mgmt.Init(FormatName:=queuePath)
            outqmgmt = mgmt.Resume()
        Next
    End Sub

    Private Sub Send_Click(sender As Object, e As EventArgs) Handles Send.Click
        myQueue.Send("Test")
        For Each queue In q.ActiveQueues
            If queue.IndexOf("DIRECT=") >= 0 Then
                outgoingQueues.Add(queue)
            End If
        Next
    End Sub
End Class

Here is the code I am using, by sending the test message to a non-existing path it gets stuck in the outgoing queue where I want to be able to call MSMQOutgoingQueueManagement.Pause or .Resume to be able to start and stop all outgoing queues.

However I keep getting an error on either mgmt.Pause() or mgmt.Resume() saying Access is denied. I can't seem to find a way to get on to the properties of outgoing queues to be able to adjust security settings. Any help would be greatly appreciated!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
R.Steele
  • 11
  • 5
  • Does [Setting MSMQ permissions for a private queue created by a different user](https://stackoverflow.com/q/4603787/1115360) give you any clues? – Andrew Morton Sep 27 '17 at 08:37
  • Andrew Morton, I have come across this and when I right clicked the outgoing message queue there was no properties option, only options to pause or resume which is what I'm trying to do programmatically. Thanks – R.Steele Sep 27 '17 at 08:45

1 Answers1

0

SOLVED!

Turns out I just needed to start up visual studio as an administrator and then it worked.

R.Steele
  • 11
  • 5