コード例 #1
0
ファイル: Form1.cs プロジェクト: mkbiltek2019/Lesons_SAG
        private void HandleCallback(IAsyncResult result)
        {
            try
            {
                SqlCommand command  = (SqlCommand)result.AsyncState;
                int        rowCount = command.EndExecuteNonQuery(result);
                string     rowText  = " rows affected.";
                if (rowCount == 1)
                {
                    rowText = " row affected.";
                }
                rowText = rowCount + rowText;

                DisplayInfoDelegate del =
                    new DisplayInfoDelegate(DisplayResults);
                this.Invoke(del, rowText);
            }
            catch (Exception ex)
            {
                this.Invoke(new DisplayInfoDelegate(DisplayStatus),
                            String.Format("Ready(last error: {0}", ex.Message));
            }
            finally
            {
                isExecuting = false;
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: zhimaqiao51/docs
        private void HandleCallback(IAsyncResult result)
        {
            try
            {
                // Retrieve the original command object, passed
                // to this procedure in the AsyncState property
                // of the IAsyncResult parameter.
                SqlCommand command  = (SqlCommand)result.AsyncState;
                int        rowCount = command.EndExecuteNonQuery(result);
                string     rowText  = " rows affected.";
                if (rowCount == 1)
                {
                    rowText = " row affected.";
                }
                rowText = rowCount + rowText;

                // You may not interact with the form and its contents
                // from a different thread, and this callback procedure
                // is all but guaranteed to be running from a different thread
                // than the form. Therefore you cannot simply call code that
                // displays the results, like this:
                // DisplayResults(rowText)

                // Instead, you must call the procedure from the form's thread.
                // One simple way to accomplish this is to call the Invoke
                // method of the form, which calls the delegate you supply
                // from the form's thread.
                DisplayInfoDelegate del = new DisplayInfoDelegate(DisplayResults);
                this.Invoke(del, rowText);
            }
            catch (Exception ex)
            {
                // Because you are now running code in a separate thread,
                // if you do not handle the exception here, none of your other
                // code catches the exception. Because none of
                // your code is on the call stack in this thread, there is nothing
                // higher up the stack to catch the exception if you do not
                // handle it here. You can either log the exception or
                // invoke a delegate (as in the non-error case in this
                // example) to display the error on the form. In no case
                // can you simply display the error without executing a delegate
                // as in the try block here.

                // You can create the delegate instance as you
                // invoke it, like this:
                this.Invoke(new DisplayInfoDelegate(DisplayStatus),
                            String.Format("Ready(last error: {0}", ex.Message));
            }
            finally
            {
                isExecuting = false;
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }
コード例 #3
0
ファイル: Brain.cs プロジェクト: Beisenbek/PP2-Summer-2018
 public Brain(DisplayInfoDelegate d)
 {
     this.d = d;
     state  = State.Zero;
 }