1

I want to send notification by using thread. I have created thread which iterate its self infinite time. Here is my code.

    public static class ThreadStartUp
{
    public static void GetThread()
    {
        var thread = new Thread(ReminderHelperClas.IterateMethod);
        thread.Start();
    }

}

MY Static class with methods is

    public static class ReminderHelperClas
{
    public static void CheckReminder()
    {
        try
        {
            const string usernamesendby = "38";
            const string message = "Please give feedback to assigned task.";
            var chathub = new ChatHub();
            using (var db = new DatabaseContext())
            {
                var datetimenow = DateTime.Now;
                const TaskAssignStatus recordstatus = (TaskAssignStatus)3;

                var remindermodel =
                    db.ActionTaskVarainceCategories.Where(x => x.TaskAssignStatus != recordstatus)
                        .Select(x => x)
                        .ToList();

                foreach (var item in remindermodel)
                {
                    if (item.ToDate == datetimenow || item.ToDate < datetimenow)
                    {
                        var username = db.Users.Where(u => u.ID == item.AssignedToID).Select(u => u.UserName).FirstOrDefault();

                        var msgclass = new TaskMessagingModel { FromId = 38, Message = message, UserName = username, ToId = item.AssignedToID };
                        SaveMessages.TaskSaveMessage(msgclass);
                        chathub.Send(usernamesendby, message, username);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public static void IterateMethod()
    {
        try
        {
            while (true)
            {
                CheckReminder();
                Thread.Sleep(10000);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

When Message is saved i called send notification method but it gives me null exception although i have provided all parameters.Please help me with this.

1 Answers1

0

You can't just initialize hub class and call its method. To get a hub to call methods on, You should use GlobalHost.ConnectionManager.GetHubContext() passing in the hub class whose object you want to get. For example

private readonly IHubContext _chatHub = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();

Then you can now use the chat hub context and call the method in the Groups or Clients property you want to send the reminder to like so

_chatHub.Clients.Client(clientConnectionString)
                        .Send(usernamesendby, message, username);

For more details, I suggest that you have a look at this tutorial http://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr

Edit The clientConnectionString is the connectionId of the particular client you want to send the message to. So the question is are you sending the message to a particular client? If to a particular client then you have to get override the OnConnected() method in the ChatHub and get and persist the connectionId either in memory or in the database, then inside your CheckReminder Method, you will now access the connectionId and use it to send the message to the particular user. You can do something like this.

 public override Task OnConnected()
        {
            string connectionId = Context.ConnectionId;
            IPrincipal user = Context.User;

            //Save connection.
            _clientManager.AddToCorrectGroup(user, connectionId;

            return base.OnConnected();
        }
Cizaphil
  • 492
  • 1
  • 5
  • 21