本文介绍了Redis的发布/订阅ServiceStack,取消线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是更普遍的螺纹的问题,我不知道。

This maybe a more general threading question i'm not sure.

不过,我有一个WPF应用程序,订阅渠道,从Redis的监听消息。数据库

But I've got a WPF app that subscribes to channels and listens for messages from a redis database.

 App.SubscriptionThread = new Thread(() =>
 {
     _redisSubscription.SubscribeToChannels("ChannelA", "ChannelB");
 });
 App.SubscriptionThread.Start();

在我开始这个我不知道如何阻止它。

Once I start this I have no idea how to stop it.

事情我已经试过了。


  1. 使用 Thread.Abort的。这显然不停止,因为线程只是进入一个坑,然后永远(无退订发生)。

  1. Using the Thread.Abort. This apparently doesn't stop it, as the thread just goes into a hang then forever (no unsubscription happens).

使用 _redisSubscription.UnSubscribeFromAllChannels()从UI线程。这也导致了应用程序了进入阻塞状态永远

Using the _redisSubscription.UnSubscribeFromAllChannels() from the UI thread. This also causes the applicaiton to go into a blocking state forever

使用强行停机时 Environment.Exit(0)。这一次似乎这样的伎俩。偏偏它也有...副作用以及...关闭我的申请。

Forcefully shutdown using Environment.Exit(0). This one seems to do the trick. Unfortunetly it also has the side effect of...well...shutting down my application.

我如何去只是停止听,这样我就可以连接/重新连接的意愿?

How do I go about just stopping the listening, so I can connect/reconnect at will?

我是不是做错了在一个新的线程启动SubscribeToChannels?

Am I doing it wrong by starting the SubscribeToChannels in a new thread?

推荐答案

这显示了如何订阅和退订的消息中的,例如:

An example that shows how to subscribe and unsubscribe from messages is in RedisMqServer, e.g:

using (var subscription = redisClient.CreateSubscription())
{
    subscription.OnUnSubscribe = channel =>
        Log.Debug("OnUnSubscribe: " + channel);

    subscription.OnMessage = (channel, msg) =>
    {
        if (msg == "STOP")
        {
            Log.Debug("Stop Command Issued");
            Log.Debug("Unsubscribing from all Channels...");
            subscription.UnSubscribeFromAllChannels(); //Unblocks thread.
        }
    };

    subscription.SubscribeToChannels(QueueNames.TopicIn); //blocks thread
}



凡使用自定义的控制消息疏通后台线程并从订阅的的onMessage 的处理程序。

这篇关于Redis的发布/订阅ServiceStack,取消线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 02:43