2017-09-27 47 views
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 

這裏是我使用的代碼,通過發送測試消息,它被卡在那裏我想傳出隊列不存在的路徑能夠調用MSMQOutgoingQueueManagement.Pause或.Resume以啓動和停止所有傳出隊列。獲取試圖暫停時訪問被拒絕的錯誤/恢復傳出消息隊列

但是,我不斷收到mgmt.Pause()或mgmt.Resume()中的錯誤,表示Access被拒絕。我似乎無法找到可以調整安全設置的傳出隊列屬性的方法。任何幫助將不勝感激!

+0

[爲其他用戶創建的專用隊列設置MSMQ權限](https://stackoverflow.com/q/4603787/1115360)是否爲您提供了任何線索? –

+0

Andrew Morton,我碰到過這個,當我右鍵點擊傳出的消息隊列時,沒有屬性選項,只有選項暫停或恢復,這是我試圖以編程方式。謝謝 –

回答

0

已解決!

原來我只是需要以管理員身份啓動visual studio,然後才能正常工作。