예제 #1
0
        public void ExecuteAsync(Action <ITikReSentence> oneResponseCallback,
                                 Action <ITikTrapSentence> errorCallback = null,
                                 Action onDoneCallback = null)
        {
            EnsureConnectionSet();
            EnsureNotRunning();
            System.Diagnostics.Debug.Assert(_asyncLoadingThread == null);

            int tag = TagSequence.Next();

            _isRuning = true;
            _asynchronouslyRunningTag = tag;

            try
            {
                string[] commandRows = ConstructCommandText(TikCommandParameterFormat.NameValue);
                _asyncLoadingThread = _connection.CallCommandAsync(commandRows, tag.ToString(),
                                                                   response =>
                {
                    ApiReSentence reResponse = response as ApiReSentence;
                    if (reResponse != null)
                    {
                        if (oneResponseCallback != null)
                        {
                            oneResponseCallback(reResponse);
                        }
                    }
                    else
                    {
                        ApiTrapSentence trapResponse = response as ApiTrapSentence;
                        if (trapResponse != null)
                        {
                            if (trapResponse.CategoryCode == "2" && trapResponse.Message == "interrupted")
                            {
                                //correct state - async operation has been Cancelled.
                            }
                            else
                            {
                                //incorrect - any error occurs
                                if (errorCallback != null)
                                {
                                    errorCallback(trapResponse);
                                }
                            }
                        }
                        else if (response is ApiDoneSentence || response is ApiFatalSentence)
                        {
                            //REMARKS: we are expecting !trap + !done sentences when any error occurs
                            _isRuning = false;
                            _asynchronouslyRunningTag = -1;
                            _asyncLoadingThread       = null;

                            if (response is ApiDoneSentence && onDoneCallback != null)
                            {
                                onDoneCallback();
                            }
                        }
                    }
                });
            }
            catch
            {
                _isRuning = false;
                _asynchronouslyRunningTag = -1;
                throw;
            }
            finally
            {
                //still running
            }
        }