[ServiceStack Redis] how to do pub and sub

157 views
Skip to first unread message

Heriyanto Binduni

unread,
Jan 13, 2012, 3:42:43 AM1/13/12
to ServiceStack .NET Open Source REST Web Services Framework
hi, i have a class like below which i include in my winform
application.

[code]
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Redis;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestRedisPubSub
{
public class RedisEx : IDisposable
{
private readonly IRedisClient redisClient;
private readonly IRedisSubscription subscription;
private readonly TextBox log;

public RedisEx(IRedisClientsManager manager, ref TextBox elog)
{
redisClient = manager.GetClient();
subscription = redisClient.CreateSubscription();
log = elog;
}

public void Start()
{
Task.Factory.StartNew(() =>
{
subscription.OnSubscribe = channel =>
{

log.Parent.Invoke(new Action(() =>
{
log.AppendText(string.Format("Subscribed to
{0} \r\n", channel));
}));
};

subscription.OnUnSubscribe = channel =>
{
log.Parent.Invoke(new Action(() =>
{
log.AppendText(string.Format("UnSubscribed to
{0} \r\n", channel));
}));
};

subscription.OnMessage = (channel, msg) =>
{
log.Parent.Invoke(new Action(() =>
{
log.AppendText(string.Format("Channel: {0}.
Message: {1} \r\n", channel, msg));
}));
};
});
}

public void Subscribe(string channel)
{
Task.Factory.StartNew(() =>
{
subscription.SubscribeToChannels(channel);
});
}

public void UnSubscribe(string channel)
{
Task.Factory.StartNew(() =>
{
subscription.UnSubscribeFromChannels(channel);
});
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (redisClient != null)
redisClient.Dispose();
if (subscription != null)
subscription.Dispose();
if (log != null)
log.Dispose();
}
}

~RedisEx()
{
Dispose(false);
}
}
}
[/code]

can you help me why sometimes i have Unresponsive Error when i close
the winform application? how to do the best way to handling redis
object dispossing in my case? i know you gave me an example in here
already:

[code]
https://github.com/ServiceStack/ServiceStack.Redis/wiki/RedisPubSub
[/code]

but is not enough for dinamic pub and sub i think.

thank you.

greeting from indonesia,
heri

Dan B

unread,
Jan 16, 2012, 5:40:55 PM1/16/12
to servic...@googlegroups.com
Try disposing the subscription then the client?

Heriyanto Binduni

unread,
Jan 17, 2012, 8:00:10 PM1/17/12
to ServiceStack .NET Open Source REST Web Services Framework
i do that already, but sometimes it still make it my application
freeze.
Reply all
Reply to author
Forward
0 new messages