Click here to Skip to main content
15,881,852 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using StringBuilder inside another class? Pin
helfri6221-Sep-09 10:00
professionalhelfri6221-Sep-09 10:00 
GeneralRe: Using StringBuilder inside another class? Pin
Luc Pattyn21-Sep-09 10:10
sitebuilderLuc Pattyn21-Sep-09 10:10 
QuestionThread reach synchronized code block Pin
FJJCENTU21-Sep-09 6:33
FJJCENTU21-Sep-09 6:33 
AnswerRe: Thread reach synchronized code block Pin
N a v a n e e t h21-Sep-09 7:01
N a v a n e e t h21-Sep-09 7:01 
Question[SOLVED] File Encryption Broken [modified] Pin
Ben Magee21-Sep-09 6:26
Ben Magee21-Sep-09 6:26 
NewsRe: File Encryption Broken Pin
Ben Magee21-Sep-09 6:41
Ben Magee21-Sep-09 6:41 
QuestionPlease help me about working with Word - Very URGENT Pin
designervc21-Sep-09 6:18
designervc21-Sep-09 6:18 
QuestionSocket programming problem Pin
JLP18821-Sep-09 6:07
JLP18821-Sep-09 6:07 
I need some help and I hope I have placed this is the correct forum.

I was asked by my employer to write an application to integrate a call recording system with a CSR issue tracking system. A customer calls in and the CSR handles the issue in the issue tracking software while at the same time a call recorder server is recording the call. My application tags the call database record with a unique id and passes it to the issue tracking software to allow administrators to later review the call recordings by clicking a link in the issue tracking software. When the link is clicked the unique call tag is passed back to my software by means of TCP sockets as in the code below to retrieve the call recording and play it to the user. I have wired an event in my code to the MessageRead event in this class. Everything works as planned EXCEPT: My problem is that when I include this class in my application I get random and frequent abends of the application with absolutely no indication of what the exception may be which caused it. I have turned on all exceptions in the debugger and tried to handle all exceptions outside of this class. This code was supplied to me by the company who wrote the issue tracking software so I have been reluctant to do anything inside their code, but at this point I am willing to try any suggestions. Their support has been to wait for us to resolve this (although I do hope and believe they are working on this on their side as well).

Any and all help is appreciated.

JLP

using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Xml;
using System.Windows.Forms;

namespace CallRecordingServer
{
    public class MessageReceiver
    {
        int tcpPort;

        private BackgroundWorker tcpThread = new BackgroundWorker();
        public delegate void ReadHandler(string MessageType, string MessageText);

        public event ReadHandler MessageRead;


        public MessageReceiver()
        {
            tcpPort = Convert.ToInt32(TaskTrayApplication.Properties.Settings.Default.tcpPort); //8083;

            tcpThread.WorkerReportsProgress = true;
            tcpThread.WorkerSupportsCancellation = true;

            tcpThread.DoWork += Thread_DoWork;
            tcpThread.ProgressChanged += tcpThread_ProgressChanged;
        }

        public void StartListening()
        {
            try
            {
                if (!tcpThread.IsBusy)
                {
                    tcpThread.RunWorkerAsync();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("MessageReceiver", ex.Message);
            }
        }

        public void StopListening()
        {
            tcpThread.CancelAsync();
            Thread.Sleep(200);
        }




        void Thread_DoWork(Object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            IPAddress ipLocalhost = IPAddress.Any;
            TcpListener tcpListener = new TcpListener(ipLocalhost, tcpPort);

            try
            {
                tcpListener.Start(100);
                while (!tcpThread.CancellationPending)
                {
                    while (!tcpListener.Pending() && !tcpThread.CancellationPending)
                    {
                        System.Windows.Forms.Application.DoEvents();
                        Thread.Sleep(10);
                    }

                    //check for cancel pending
                    if (tcpThread.CancellationPending)
                    {
                        break;
                    }

                    //open stream and receive data
                    TcpClient tcpClient = tcpListener.AcceptTcpClient();

                    NetworkStream netStream = tcpClient.GetStream();
                    StreamReader netStreamReader = new StreamReader(netStream);
                    StreamWriter netStreamWriter = new StreamWriter(netStream);
                    netStreamWriter.AutoFlush = true;
                    string stringData;

                    //report thread progress
                    stringData = netStreamReader.ReadToEnd();
                    tcpThread.ReportProgress(0, stringData);

                    //close stream
                    netStreamReader.Close();
                    netStream.Close();
                    
                    tcpClient.Close();

                }
            }

            catch (Exception ex)
            {

                //handle the exception outside the thread
                MessageBox.Show("MessageReceiver",ex.Message);
                tcpThread.ReportProgress(100, e);
            }
            finally
            {


                //stop the listener
                tcpListener.Stop();

            }

        }



        void tcpThread_ProgressChanged(Object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 0)
            {
                //convert the xml to an object and fire public event
                try
                {
                    string xmlData = e.UserState.ToString();

                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(xmlData);
                    XmlNode root = doc.FirstChild;
                    string messageType = root.ChildNodes[0].InnerText;
                    string messageText = root.ChildNodes[1].InnerText;
                    MessageRead(messageType, messageText);
                }
                catch (Exception ex)
                {
                    string er = ex.Message;
                    MessageBox.Show(ex.Message);
                    //do nothing because we're disposing
                }
            }
            else
            {
                if (e.ProgressPercentage == 100)
                {
                    //could not connect
                    //displayError("Error with TCP connection: " & CType(e.UserState, Exception).Message)
                    tcpThread.CancelAsync();
                    Thread.Sleep(200);
                }
            }
        }

    }
}

QuestionRecovering delivery Errors sending SMTP Mails Pin
FJJCENTU21-Sep-09 4:14
FJJCENTU21-Sep-09 4:14 
QuestionCreate a delegate at run time Pin
bonzaiholding21-Sep-09 2:50
bonzaiholding21-Sep-09 2:50 
AnswerRe: Create a delegate at run time Pin
Not Active21-Sep-09 4:04
mentorNot Active21-Sep-09 4:04 
QuestionCreating Registry Keys with Setup if key doesn't exist. Pin
Paramhans Dubey21-Sep-09 2:50
professionalParamhans Dubey21-Sep-09 2:50 
AnswerRe: Creating Registry Keys with Setup if key doesn't exist. Pin
Manas Bhardwaj21-Sep-09 3:24
professionalManas Bhardwaj21-Sep-09 3:24 
GeneralRe: Creating Registry Keys with Setup if key doesn't exist. Pin
Paramhans Dubey21-Sep-09 19:06
professionalParamhans Dubey21-Sep-09 19:06 
QuestionReportViewer control problem Pin
firefeet21-Sep-09 1:23
firefeet21-Sep-09 1:23 
AnswerRe: ReportViewer control problem Pin
Abhishek Sur21-Sep-09 11:42
professionalAbhishek Sur21-Sep-09 11:42 
GeneralRe: ReportViewer control problem Pin
firefeet21-Sep-09 18:16
firefeet21-Sep-09 18:16 
QuestionHow to delete outlook appointment Pin
tspradeep198021-Sep-09 1:06
tspradeep198021-Sep-09 1:06 
Questionsplitting values randomly - C# Pin
flower_t21-Sep-09 1:05
flower_t21-Sep-09 1:05 
AnswerRe: splitting values randomly - C# [modified] Pin
Calla21-Sep-09 1:29
Calla21-Sep-09 1:29 
AnswerRe: splitting values randomly - C# Pin
OriginalGriff21-Sep-09 2:46
mveOriginalGriff21-Sep-09 2:46 
AnswerRe: splitting values randomly - C# Pin
flower_t21-Sep-09 2:56
flower_t21-Sep-09 2:56 
GeneralRe: splitting values randomly - C# Pin
harold aptroot21-Sep-09 3:38
harold aptroot21-Sep-09 3:38 
GeneralRe: splitting values randomly - C# Pin
PIEBALDconsult21-Sep-09 7:39
mvePIEBALDconsult21-Sep-09 7:39 
AnswerRe: splitting values randomly - C# Pin
Luc Pattyn21-Sep-09 4:10
sitebuilderLuc Pattyn21-Sep-09 4:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.