Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have the following class. What I want to do is to trigger some events based on the output from server. But Threading seems to take only static classes. and I dont want to make my Eventhandlers static. So, Its not accessible to the static method... How can I solve this issue... please...

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading;

namespace AstWrapper
{
    public class Manager
    {
        //Events
        public event EventMaker.HangupEvent OnHangup;
        public event EventMaker.NewStateEvent NewStateEvent;

        Thread manthread = new Thread(new ThreadStart(getdata));
        static Queue<string> res_q = new Queue<string>(); 

        public const string LINE_SEPARATOR = "\r\n";

        private static Socket ManSock = null; 

        public string Ast_Server { get; set; }
        public int Ast_Port { get; set; }
        public string Ast_Username { get; set; }
        public string Ast_Password { get; set; }

        public Manager()
        {
        }

        public Manager(string server, int port, string username, string password)
        {
            Ast_Server = server;
            Ast_Port = port;
            Ast_Username = username;
            Ast_Password = password; 
        }
        public bool connect()
        {
            try
            {
                ManSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(Ast_Server), Ast_Port);
                ManSock.Connect(serverEndPoint);
                if (ManSock.Connected)
                    return true;
                else
                    return false; 
            }
            catch
            {
                return false;
            }

         
        }
        private static  void getdata()
        {
            do
            {
              
                int bytesRead = 0;
                byte[] buffer = new byte[1024];
                bytesRead = ManSock.Receive(buffer);

                string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                if (response != "")
                {
                    string[] pp = response.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                    foreach (string s in pp)
                    {
                        if (s.Trim().StartsWith("Event:"))
                         //Here I wanna call the event
                    }
                    
                }
                Thread.Sleep(1000);   
            } while (ManSock.Connected);

        }

        public void  Login()
        {
           
            ManSock.Send(Encoding.ASCII.GetBytes("Action: Login" +  LINE_SEPARATOR + "Username: " + Ast_Username + LINE_SEPARATOR + "Secret: " + Ast_Password + LINE_SEPARATOR + LINE_SEPARATOR));
         
            //Console.WriteLine(bytesRead + " bytes from asterisk server.");
             manthread.Start(); 
          
           

            
        }

    }
}
Posted
Updated 18-Aug-13 0:47am
v3

1 solution

The ThredStart is just an easy approach. Look here for a more complex example, that is using worker thread class model: http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.90).aspx[^].

You could also take a look at the newer features like BackgroundWorker[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900