private void RemoteService_ExecCompleted(object sender, ExecCompletedEventArgs e)
        {
            try
            {
                IsRequestInProgress = false;

                RequestDetails currentResponseRequest = _activeRequestDetails;
                _activeRequestDetails = null; // Doing this now instead of at end, as code called by this method might kick off a new request

                if (e.Error != null)
                {
                    if (currentResponseRequest.ResponseCallBack != null)
                        currentResponseRequest.ResponseCallBack(false, null);

                    ApplicationEx.Instance.DisplayMessageBox(e.Error.Message, "Error Message");

                    _chunking = false;
                    RequestQueue.Clear(); // Future requests made invalid due to error, so clear the queue. TODO: Determine whether there is a better strategy for dealing with queued requests after an error.

                    return;
                }

                if (!string.IsNullOrEmpty(e.errors))
                {
                    if (currentResponseRequest.ResponseCallBack != null)
                        currentResponseRequest.ResponseCallBack(false, null);

                    ApplicationEx.Instance.DisplayMessageBox(e.errors, "Server Message");

                    _chunking = false;
                    RequestQueue.Clear(); // Future requests made invalid due to error, so clear the queue. TODO: Determine whether there is a better strategy for dealing with queued requests after an error.

                    return;
                }

                if (e.Result.Length > 0)
                {
                    Response = XDocument.Parse(e.Result);

                    // Write the message to the output window
            //                    System.Diagnostics.Debug.WriteLine("RAW RESPONSE:");

            //#if WINDOWS_PHONE
            //                    int startPos = 0;

            //                    string debugString = Response.ToString();

            //                    while (startPos < debugString.Length)
            //                    {
            //                        if (startPos + 700 > e.Result.Length)
            //                            System.Diagnostics.Debug.WriteLine(debugString.Substring(startPos));
            //                        else
            //                            System.Diagnostics.Debug.WriteLine(debugString.Substring(startPos, 700));

            //                        startPos += 700;
            //                    }
            //#else
            //                    System.Diagnostics.Debug.WriteLine(Response.ToString());
            //#endif

                    if (ServerResponse != null)
                        ServerResponse(Response.ToString());

                    bool lastSuccess = Common.boolValue(Response.Document.Root.Attribute(Common.ResultSuccess).Value);

                    if (Response.Document.Root.Attribute(Common.serverMsg) != null)
                    {
                        string msg = Response.Document.Root.Attribute(Common.serverMsg).Value;

                        if (msg.Length > 0)
                        {
                            ApplicationEx.Instance.DisplayMessageBox(msg, "Server Broadcast Message");
                            Response.Document.Root.Attribute(Common.serverMsg).Remove();
                        }
                    }

                    #region Chunking
                    if (Response.Document.Root.Attribute(Common.Chunking.chunking) != null &&
                        Common.boolValue(Response.Document.Root.Attribute(Common.Chunking.chunking).Value))
                    {
                        this._chunking = true;

                        // Create cancel window
                        _chunkProgressText = Response.Document.Root.Attribute(Common.Chunking.chunkProgress).Value;
                        string progress = Response.Document.Root.Attribute(Common.Chunking.chunkProgressPercentage).Value;
                        _chunkProgressPercentage = 0;

                        if (progress.Length > 0)
                            int.TryParse(progress, out _chunkProgressPercentage);

                        if (_cancelChunkForm == null && !_chunkThreadStarted)
                        {
                            _chunkingCancelled = false;
                            //Thread t = new Thread(new ThreadStart(createLongOperationCancelForm));
                            //t.SetApartmentState( ApartmentState.STA);
                            //t.Priority = ThreadPriority.AboveNormal;
                            //t.Start();
                            _chunkThreadStarted = true;
                        }

                        if (_cancelChunkForm != null)
                        {
                            _cancelChunkForm.Description = _chunkProgressText;
                            _cancelChunkForm.Progress = _chunkProgressPercentage;
                            _chunkThreadStarted = false;
                        }
                    }
                    else if (_chunking)
                    {
                        this._chunking = false;
                        if (_cancelChunkForm != null)
                        {
                            _cancelChunkForm.Close();
                            _cancelChunkForm = null;
                        }
                    }

                    if (_chunking)
                    {
                        Request.Document.Root.RemoveAll();
                        XElement elt;

                        if (_cancelChunkForm != null && this._chunkingCancelled)
                        {
                            elt = new XElement(Common.Chunking.CancelChunk);
                            _cancelChunkForm.Close();
                            _cancelChunkForm = null;
                        }
                        else
                        {
                            elt = new XElement(Common.Chunking.KeepChunking);
                        }

                        Request.Document.Add(elt);
                    }
                }
                #endregion

                if (currentResponseRequest.ResponseCallBack != null)
                    currentResponseRequest.ResponseCallBack(true, Response);

                if (!_chunking)
                {
                    if (!_dontAutoPublishResponse)
                        PublishResponse(new XDocument(Response)); // NOTE: Doing a new in order to get a deep copy, not affected by other actions on it while it's being parsed
                }
            }
            finally
            {
                ApplicationEx.Instance.IsBusy = false;

                // Now that this request has been processed, move to the next entry in the queue
                if (RequestQueue.Count != 0)
                    SendRequestToServerPortal(RequestQueue.Dequeue());
            }
        }
        private void RemoteService_ExecCompleted(object sender, ExecCompletedEventArgs e)
        {
            try
            {
                IsRequestInProgress = false;

                RequestDetails currentResponseRequest = _activeRequestDetails;
                _activeRequestDetails = null; // Doing this now instead of at end, as code called by this method might kick off a new request

                if (e.Error != null)
                {
                    if (currentResponseRequest.ResponseCallBack != null)
                    {
                        currentResponseRequest.ResponseCallBack(false, null);
                    }

                    ApplicationEx.Instance.DisplayMessageBox(e.Error.Message, "Error Message");

                    _chunking = false;
                    RequestQueue.Clear(); // Future requests made invalid due to error, so clear the queue. TODO: Determine whether there is a better strategy for dealing with queued requests after an error.

                    return;
                }

                if (!string.IsNullOrEmpty(e.errors))
                {
                    if (currentResponseRequest.ResponseCallBack != null)
                    {
                        currentResponseRequest.ResponseCallBack(false, null);
                    }

                    ApplicationEx.Instance.DisplayMessageBox(e.errors, "Server Message");

                    _chunking = false;
                    RequestQueue.Clear(); // Future requests made invalid due to error, so clear the queue. TODO: Determine whether there is a better strategy for dealing with queued requests after an error.

                    return;
                }

                if (e.Result.Length > 0)
                {
                    Response = XDocument.Parse(e.Result);

                    // Write the message to the output window
//                    System.Diagnostics.Debug.WriteLine("RAW RESPONSE:");

//#if WINDOWS_PHONE
//                    int startPos = 0;

//                    string debugString = Response.ToString();

//                    while (startPos < debugString.Length)
//                    {
//                        if (startPos + 700 > e.Result.Length)
//                            System.Diagnostics.Debug.WriteLine(debugString.Substring(startPos));
//                        else
//                            System.Diagnostics.Debug.WriteLine(debugString.Substring(startPos, 700));

//                        startPos += 700;
//                    }
//#else
//                    System.Diagnostics.Debug.WriteLine(Response.ToString());
//#endif

                    if (ServerResponse != null)
                    {
                        ServerResponse(Response.ToString());
                    }

                    bool lastSuccess = Common.boolValue(Response.Document.Root.Attribute(Common.ResultSuccess).Value);

                    if (Response.Document.Root.Attribute(Common.serverMsg) != null)
                    {
                        string msg = Response.Document.Root.Attribute(Common.serverMsg).Value;

                        if (msg.Length > 0)
                        {
                            ApplicationEx.Instance.DisplayMessageBox(msg, "Server Broadcast Message");
                            Response.Document.Root.Attribute(Common.serverMsg).Remove();
                        }
                    }

                    #region Chunking
                    if (Response.Document.Root.Attribute(Common.Chunking.chunking) != null &&
                        Common.boolValue(Response.Document.Root.Attribute(Common.Chunking.chunking).Value))
                    {
                        this._chunking = true;

                        // Create cancel window
                        _chunkProgressText = Response.Document.Root.Attribute(Common.Chunking.chunkProgress).Value;
                        string progress = Response.Document.Root.Attribute(Common.Chunking.chunkProgressPercentage).Value;
                        _chunkProgressPercentage = 0;

                        if (progress.Length > 0)
                        {
                            int.TryParse(progress, out _chunkProgressPercentage);
                        }

                        if (_cancelChunkForm == null && !_chunkThreadStarted)
                        {
                            _chunkingCancelled = false;
                            //Thread t = new Thread(new ThreadStart(createLongOperationCancelForm));
                            //t.SetApartmentState( ApartmentState.STA);
                            //t.Priority = ThreadPriority.AboveNormal;
                            //t.Start();
                            _chunkThreadStarted = true;
                        }

                        if (_cancelChunkForm != null)
                        {
                            _cancelChunkForm.Description = _chunkProgressText;
                            _cancelChunkForm.Progress    = _chunkProgressPercentage;
                            _chunkThreadStarted          = false;
                        }
                    }
                    else if (_chunking)
                    {
                        this._chunking = false;
                        if (_cancelChunkForm != null)
                        {
                            _cancelChunkForm.Close();
                            _cancelChunkForm = null;
                        }
                    }

                    if (_chunking)
                    {
                        Request.Document.Root.RemoveAll();
                        XElement elt;

                        if (_cancelChunkForm != null && this._chunkingCancelled)
                        {
                            elt = new XElement(Common.Chunking.CancelChunk);
                            _cancelChunkForm.Close();
                            _cancelChunkForm = null;
                        }
                        else
                        {
                            elt = new XElement(Common.Chunking.KeepChunking);
                        }

                        Request.Document.Add(elt);
                    }
                }
                #endregion

                if (currentResponseRequest.ResponseCallBack != null)
                {
                    currentResponseRequest.ResponseCallBack(true, Response);
                }

                if (!_chunking)
                {
                    if (!_dontAutoPublishResponse)
                    {
                        PublishResponse(new XDocument(Response)); // NOTE: Doing a new in order to get a deep copy, not affected by other actions on it while it's being parsed
                    }
                }
            }
            finally
            {
                ApplicationEx.Instance.IsBusy = false;

                // Now that this request has been processed, move to the next entry in the queue
                if (RequestQueue.Count != 0)
                {
                    SendRequestToServerPortal(RequestQueue.Dequeue());
                }
            }
        }