0

2 つのテスト例を使用して、C# で tib rv msg (7.5.3 を使用) をリッスンしようとしています。1 つは純粋な C# コンソール アプリで、もう 1 つは C# WPF アプリです。私が見つけたのは、コンソールアプリは正常に動作しますが、WPF はしばらくするとメッセージを取得しなくなります (50 メッセージ後、30 メッセージ後などによって異なります)。

コンソール アプリ コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TIBCO.Rendezvous;

using System.Threading;

namespace testTibcoListener
{
class Program
{
    static int count = 0;
    static void Main(string[] args)
    {

        TIBCO.Rendezvous.Environment.Open();
        TIBCO.Rendezvous.Transport aa = new NetTransport(service, network, daemon);
        Connection cn = new Connection("TEST.>", aa);
        cn.MessageReceived += OnDeleteOrderMessageReceived;
        WaitCallback wc = (o) =>
        {
            while (true)
            {
                Queue.Default.TimedDispatch(0);  
            }
        };

        ThreadPool.QueueUserWorkItem(wc);

        while (true)
        {
            Thread.Sleep(200);
        }
    }

    static private void OnDeleteOrderMessageReceived(object listener, MessageReceivedEventArgs args)
    {
        TIBCO.Rendezvous.Message msg = args.Message;
        Console.WriteLine(count);
        count++;
    }
}

public class Connection : IDisposable
{
    private string sendSubject;
    private string inbox;
    private TIBCO.Rendezvous.Transport transport;
    private TIBCO.Rendezvous.Listener listener;

    public Connection(string sendSubject,
                      Transport transport)
    {
        this.sendSubject = sendSubject;
        this.transport = transport;
        this.inbox = this.transport.CreateInbox();
        this.listener = new TIBCO.Rendezvous.Listener(
          TIBCO.Rendezvous.Queue.Default, this.transport, this.sendSubject, null);
    }
    public event TIBCO.Rendezvous.MessageReceivedEventHandler MessageReceived
    {
        add { this.listener.MessageReceived += value; }
        remove { this.listener.MessageReceived -= value; }
    }


    public void Send(TIBCO.Rendezvous.Message msg)
    {
        try
        {
            this.transport.Send(msg);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);
        }
    }

    #region IDisposable Members
    public void Dispose()
    {
        this.listener.Destroy();
    }
    #endregion
}
}

WPF アプリ コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TIBCO.Rendezvous;
using System.ComponentModel;
using System.Threading;
using System.Windows.Threading;

namespace WpfApplication2
{
public partial class Window1 : Window
{
    public int count_;
    public Window1()
    {
        InitializeComponent();
        count_ = 0;

        TIBCO.Rendezvous.Environment.Open();
        TIBCO.Rendezvous.NetTransport aa = new NetTransport(service, network, daemon);
        Connection cn = new Connection("TEST.>", aa, this);
        cn.MessageReceived += OnDeleteOrderMessageReceived;
        WaitCallback wc = (o) =>
        {          
            while (true)
            {   
                    Queue.Default.TimedDispatch(0);       //          
            }
        };

        ThreadPool.QueueUserWorkItem(wc);
    }

    private void OnDeleteOrderMessageReceived(object listener, MessageReceivedEventArgs args)
    {
        TIBCO.Rendezvous.Message msg = args.Message;
        int aaa = 1111;

        aaa = msg.FieldCountAsInt;

        this.textBox1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
             new DispatcherOperationCallback(delegate
             {

                 int a = 1;// rd.Next(2);             
                 this.count_++;
                 if (a == 1)
                 {
                     this.textBox1.Text = "total count: " + System.Convert.ToString(this.count_) + ",queue: " + System.Convert.ToString(Queue.Default.Count);
                 }
                 else
                 {
                     this.textBox1.Text = "11111";
                     a = 1;
                 }
                 return null;
             }), null);


    }

}
public class Connection : IDisposable
{
    private string sendSubject;
    private string inbox;
    private TIBCO.Rendezvous.Transport transport;
    private TIBCO.Rendezvous.Listener listener;
    private WpfApplication2.Window1 myWindow;
    public Connection(string sendSubject,
                      Transport transport,WpfApplication2.Window1 window)
    {
        this.sendSubject = sendSubject;
        this.transport = transport;
        this.inbox = this.transport.CreateInbox();
        this.listener = new TIBCO.Rendezvous.Listener(
          TIBCO.Rendezvous.Queue.Default, this.transport, this.sendSubject, null);
        this.myWindow = window;           
    }
    public event TIBCO.Rendezvous.MessageReceivedEventHandler MessageReceived
    {
        add { this.listener.MessageReceived += value; }
        remove { this.listener.MessageReceived -= value; }
    }


    public void Send(TIBCO.Rendezvous.Message msg)
    {
        try
        {
            this.transport.Send(msg);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);
        }
    }

    #region IDisposable Members
    public void Dispose()
    {
        this.listener.Destroy();
    }
    #endregion 
}
}

WPF コードでは、Queue.Default.TimedDispatch(0); キューからメッセージをディスパッチしない場所です。キューにメッセージがないようです。

4

1 に答える 1

0

2 つの提案:

1) ThreadPool.QueueUserWorkItem の代わりに BackgroundWorker を使用して、バックグラウンド スレッドが使用され、UI スレッドが不足していないことを確認します。

2) UI スレッドで while ループを実行することになった場合は、WPF DoEvents ループを使用するか、単に System.Windows.Forms.Application.DoEvents() を呼び出して、メッセージ ループが実行されるようにします。

于 2011-06-28T17:41:08.137 に答える