public IAsyncResult StartWrite(string s, int offset, int length, AsyncCallback cb, object o)
            {
                myAsyncResult mar = new myAsyncResult(cb, o);

                // Allow for exception throwing to test our handling of that.
                if (s == null)
                {
                    throw new ArgumentNullException("s");
                }

                Task t = Task.Factory.StartNew(delegate
                {
                    try
                    {
                        lock (_list)
                        {
                            for (int i = 0; i < length; i++)
                            {
                                _list.Add(s[i + offset]);
                            }
                        }
                        mar.Signal();
                    }
                    catch (Exception e) { mar.Signal(e); }
                });


                return(mar);
            }
            public string EndRead(IAsyncResult iar)
            {
                myAsyncResult mar = iar as myAsyncResult;

                if (mar.IsFaulted)
                {
                    throw (mar.Exception);
                }
                return((string)mar.AsyncState);
            }
            public void EndWrite(IAsyncResult iar)
            {
                myAsyncResult mar = iar as myAsyncResult;

                mar.Wait();
                if (mar.IsFaulted)
                {
                    throw (mar.Exception);
                }
            }
Esempio n. 4
0
            public IAsyncResult StartRead(int maxBytes, char[] buf, int offset, AsyncCallback cb, object o)
            {
                myAsyncResult mar = new myAsyncResult(cb, o);

                // Allow for exception throwing to test our handling of that.
                if (maxBytes == -1)
                {
                    throw new ArgumentException("Value was not valid", nameof(maxBytes));
                }

                Task t = Task.Factory.StartNew(delegate
                {
                    //Thread.Sleep(100);
                    StringBuilder sb = new StringBuilder();
                    int bytesRead    = 0;
                    try
                    {
                        lock (_list)
                        {
                            while ((_list.Count > 0) && (bytesRead < maxBytes))
                            {
                                sb.Append(_list[0]);
                                if (buf != null)
                                {
                                    buf[offset] = _list[0]; offset++;
                                }
                                _list.RemoveAt(0);
                                bytesRead++;
                            }
                        }

                        mar.SignalState(sb.ToString());
                    }
                    catch (Exception e) { mar.Signal(e); }
                });

                return(mar);
            }
            public IAsyncResult StartRead(int maxBytes, char[] buf, int offset, AsyncCallback cb, object o)
            {
                myAsyncResult mar = new myAsyncResult(cb, o);

                // Allow for exception throwing to test our handling of that.
                if (maxBytes == -1)
                    throw new ArgumentException("maxBytes");

                Task t = Task.Factory.StartNew(delegate
                {
                    //Thread.Sleep(100);
                    StringBuilder sb = new StringBuilder();
                    int bytesRead = 0;
                    try
                    {
                        lock (_list)
                        {
                            while ((_list.Count > 0) && (bytesRead < maxBytes))
                            {
                                sb.Append(_list[0]);
                                if (buf != null) { buf[offset] = _list[0]; offset++; }
                                _list.RemoveAt(0);
                                bytesRead++;
                            }
                        }

                        mar.SignalState(sb.ToString());
                    }
                    catch (Exception e) { mar.Signal(e); }
                });

                return mar;
            }
            public IAsyncResult StartWrite(string s, int offset, int length, AsyncCallback cb, object o)
            {
                myAsyncResult mar = new myAsyncResult(cb, o);

                // Allow for exception throwing to test our handling of that.
                if (s == null)
                    throw new ArgumentNullException("s");

                Task t = Task.Factory.StartNew(delegate
                {
                    //Task.Delay(100).Wait();
                    try
                    {
                        lock (_list)
                        {
                            for (int i = 0; i < length; i++)
                                _list.Add(s[i + offset]);
                        }
                        mar.Signal();
                    }
                    catch (Exception e) { mar.Signal(e); }
                });


                return mar;
            }