コード例 #1
0
ファイル: frmDisk.cs プロジェクト: WeslieRoco/Roco_Shell
 private void chkdsk_Click(object sender, EventArgs e)
 {
     //docmd("chkdsk.exe", disk + ":");
     ParameterizedThreadStart p = new ParameterizedThreadStart(docmd2);
     IAsyncResult i=p.BeginInvoke(new string[] { "chkdsk", disk + ":" }, null, null);
     //p.EndInvoke(i);
 }
コード例 #2
0
        // 发送文件 - 异步方法
        public void BeginSendFile(string filePath)
        {
            ParameterizedThreadStart start =
                new ParameterizedThreadStart(BeginSendFile);

            start.BeginInvoke(filePath, null, null);
        }
コード例 #3
0
        /// <summary>
        /// Extends BeginInvoke so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// parameterizedthreadstart.BeginInvoke(obj, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginInvoke(this ParameterizedThreadStart parameterizedthreadstart, Object obj, AsyncCallback callback)
        {
            if (parameterizedthreadstart == null)
            {
                throw new ArgumentNullException("parameterizedthreadstart");
            }

            return(parameterizedthreadstart.BeginInvoke(obj, callback, null));
        }
コード例 #4
0
 public ClientListener()
     : base()
 {
     listener_async = new AsyncCallback(AsyncResult_listener);
     listener_start = new ParameterizedThreadStart(Listener);
     listener_start.BeginInvoke(this, listener_async, this);
     listener = new Thread(listener_start);
     base.Start();
 }
コード例 #5
0
        public void BeginSendFile(string filePath)
        {
#if NETCOREAPP2_0
            beginSendFile(filePath);
#else
            ParameterizedThreadStart start = new ParameterizedThreadStart(beginSendFile);
            start.BeginInvoke(filePath, null, null);
#endif
        }
コード例 #6
0
        /// <summary>
        /// Load specified file and return instance
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static LoadedDocument Load(string filename)
        {
            ParameterizedThreadStart threadStart = ThreadLoader.Load;

            iAsyncResult = threadStart.BeginInvoke(filename, null, null);

            //do other things

            threadStart.EndInvoke(iAsyncResult);

            return(Document);
        }
コード例 #7
0
ファイル: Server.cs プロジェクト: FeiYanLeung/Laboratory
        /// <summary>
        /// 在读取完成时进行回调
        /// </summary>
        /// <param name="ar"></param>
        private void OnReadComplete(IAsyncResult ar)
        {
            int bytesRead = 0;

            try
            {
                bytesRead = streamToClient.EndRead(ar);
                Console.WriteLine($"Reading data, {bytesRead} bytes.");

                if (bytesRead == 0)
                {
                    Console.WriteLine("Client offline.");
                    return;
                }

                string msg = Encoding.Unicode.GetString(buffer, 0, bytesRead);

                // 清空缓存,避免脏读
                Array.Clear(buffer, 0, buffer.Length);

                // 获取protocol数组
                string[] protocolArray = handler.GetProtocol(msg);
                foreach (var pro in protocolArray)
                {
#if NETCOREAPP2_0
                    handlerProtocol(pro);
#else
                    // 异步读取,避免阻塞
                    ParameterizedThreadStart start = new ParameterizedThreadStart(handlerProtocol);
                    start.BeginInvoke(pro, null, null);
#endif
                }

                // 再次调用BeginRead(), 完成时调用自身,形成无限循环
                AsyncCallback callback = new AsyncCallback(OnReadComplete);
                streamToClient.BeginRead(buffer, 0, bufferSize, callback, null);
            }
            catch (Exception e)
            {
                if (streamToClient != null)
                {
                    streamToClient.Dispose();
                }

                client.Close();

                Console.WriteLine(e.Message);
            }
        }
コード例 #8
0
        // 再读取完成时进行回调
        private void OnReadComplete(IAsyncResult ar)
        {
            int bytesRead = 0;

            try
            {
                lock (streamToClient)
                {
                    bytesRead = streamToClient.EndRead(ar);
                    Console.WriteLine("Reading data, {0} bytes ...", bytesRead);
                }
                if (bytesRead == 0)
                {
                    throw new Exception("读取到0字节");
                }

                string msg = Encoding.Unicode.GetString(buffer, 0, bytesRead);
                Array.Clear(buffer, 0, buffer.Length);        // 清空缓存,避免脏读

                // 获取protocol数组
                string[] protocolArray = handler.GetProtocal(msg);
                foreach (string pro in protocolArray)
                {
                    // 这里异步调用,不然这里可能会比较耗时
                    ParameterizedThreadStart start =
                        new ParameterizedThreadStart(handleProtocol);
                    start.BeginInvoke(pro, null, null);
                }

                // 再次调用BeginRead(),完成时调用自身,形成无限循环
                lock (streamToClient)
                {
                    AsyncCallback callBack = new AsyncCallback(OnReadComplete);
                    streamToClient.BeginRead(buffer, 0, BufferSize, callBack, null);
                }
            }
            catch (Exception ex)
            {
                if (streamToClient != null)
                {
                    streamToClient.Dispose();
                }
                client.Close();
                Console.WriteLine(ex.Message);      // 捕获异常时退出程序
            }
        }
コード例 #9
0
ファイル: MetaDataBrowser.cs プロジェクト: zhh007/MyGen
        public void SetupAsync()
        {
            if (IsTreeBusy)
            {
                return;
            }

            this.toolBar1.Enabled = false;

            this.Text = "MyMeta Browser (" + DefaultSettings.Instance.DbDriver + ")";

            static_rootNode = new TreeNode();

            this.MyTree.BeginUpdate();
            this.MyTree.Nodes.Clear();
            this.MyTree.Nodes.Add(loadingNode);
            this.MyTree.EndUpdate();
            this.MyTree.Invalidate();

            loadingNode.ImageIndex         = 26;
            loadingNode.SelectedImageIndex = loadingNode.ImageIndex;
            this.MyTree.Invalidate();

            timerIconAnimate.Start();

            // RESET MYMETA!
            StaticMyMeta = null;

            AsyncLoadInfo ali = new AsyncLoadInfo();

            ali.RootNode           = static_rootNode;
            ali.ShowSystemEntities = this.chkSystem.Checked;
            ali.Error = string.Empty;

            this.Invalidate();
            this.Refresh();

            ParameterizedThreadStart ts = new ParameterizedThreadStart(SetupAndBuildNodeTree);

            asyncres = ts.BeginInvoke(ali, new AsyncCallback(SetupAsyncCompleted), null);
        }
コード例 #10
0
        protected override void ReadComplete(IAsyncResult ar)
        {
            try
            {
                var stream = (NetworkStream)ar.AsyncState;
                if (Client.IsOnlineRead())
                {
                    var length = stream.EndRead(ar);
                    OnLog(this, string.Format("读取到 {0} 字节 ...", length));
                    if (length < 2)
                    {
                        return;
                    }
                    if (length == 8)
                    {
                        Stop();
                        OnClientDisconnect(this);
                        return;
                    }

                    var comms = Handler.GetCommand(Buffer, length);
                    foreach (Record comm in comms)
                    {
                        ParameterizedThreadStart start = HandleProtocol;
                        start.BeginInvoke(comm, null, null);
                    }
                    // 再次调用BeginRead(),完成时调用自身,形成无限循环
                    stream.BeginRead(Buffer, 0, BufferSize, ReadComplete, stream);
                }
                else
                {
                    OnClientDisconnect(this);
                }
            }
            catch (Exception e)
            {
                OnLog(this, e.Message);
            }
        }
コード例 #11
0
ファイル: BaseClient.cs プロジェクト: TruthLiAng/someLearn
        /// <summary>
        /// 读取发送内容
        /// </summary>
        /// <param name="ar">异步操作</param>
        protected virtual void ReadComplete(IAsyncResult ar)
        {
            try
            {
                var stream = (NetworkStream)ar.AsyncState;
                if (Client.IsOnlineRead())
                {
                    var bytesRead = stream.EndRead(ar);
                    Loghelper.Debug(string.Format("读取到 {0} 字节 ...", bytesRead));
                    if (bytesRead == 0)
                    {
                        return;
                    }

                    string msg = Coding.GetString(Buffer, 0, bytesRead);
                    Array.Clear(Buffer, 0, Buffer.Length); // 清空缓存,避免脏读

                    var comms = Handler.GetCommand(msg);
                    foreach (Record comm in comms)
                    {
                        ParameterizedThreadStart start = HandleProtocol;
                        start.BeginInvoke(comm, null, null);
                    }
                    // 再次调用BeginRead(),完成时调用自身,形成无限循环
                    stream.BeginRead(Buffer, 0, BufferSize, ReadComplete, stream);
                }
                else
                {
                    OnClientDisconnect(this);
                }
            }
            catch (Exception e)
            {
                Loghelper.Error(e);
            }
        }
コード例 #12
0
        public void StartExecutePlugins(Build build)
        {
            ParameterizedThreadStart threadStart = ExecAsync;

            threadStart.BeginInvoke(build, null, null);
        }
コード例 #13
0
        public void SetupAsync()
        {
            if (IsTreeBusy) return;

            this.toolBar1.Enabled = false;

            this.Text = "MyMeta Browser (" + DefaultSettings.Instance.DbDriver + ")";

            static_rootNode = new TreeNode();

            this.MyTree.BeginUpdate();
            this.MyTree.Nodes.Clear();
            this.MyTree.Nodes.Add(loadingNode);
            this.MyTree.EndUpdate();
            this.MyTree.Invalidate();

            loadingNode.ImageIndex = 26;
            loadingNode.SelectedImageIndex = loadingNode.ImageIndex;
            this.MyTree.Invalidate();

            timerIconAnimate.Start();

            // RESET MYMETA!
            StaticMyMeta = null;

            AsyncLoadInfo ali = new AsyncLoadInfo();
            ali.RootNode = static_rootNode;
            ali.ShowSystemEntities = this.chkSystem.Checked;
            ali.Error = string.Empty;

            this.Invalidate();
            this.Refresh();

            ParameterizedThreadStart ts = new ParameterizedThreadStart(SetupAndBuildNodeTree);
            asyncres = ts.BeginInvoke(ali, new AsyncCallback(SetupAsyncCompleted), null);
        }
コード例 #14
0
 public void ExecuteScripts(IEnumerable<Script> scripts)
 {
     IsExecuting = true;
     ExecutionDelegate = ExecuteScriptsInternal;
     ExecutionDelegate.BeginInvoke(scripts, ExecuteScriptsCallback, null);
 }