当前位置: 首页>>代码示例>>C#>>正文


C# StreamReader.EnumerateLines方法代码示例

本文整理汇总了C#中System.IO.StreamReader.EnumerateLines方法的典型用法代码示例。如果您正苦于以下问题:C# StreamReader.EnumerateLines方法的具体用法?C# StreamReader.EnumerateLines怎么用?C# StreamReader.EnumerateLines使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.StreamReader的用法示例。


在下文中一共展示了StreamReader.EnumerateLines方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: StartObservableStream

        /// <summary>
        /// Starts the stream.
        /// </summary>
        /// <returns>The observable object.</returns>
        /// <param name="e">Tokens.</param>
        /// <param name="type">Type of streaming API.</param>
        /// <param name="parameters">Parameters.</param>
        public static IObservable<StreamingMessage> StartObservableStream(this StreamingApi e, StreamingType type, StreamingParameters parameters = null)
        {
            if(parameters == null)
                parameters = new StreamingParameters();

            return Observable.Create<StreamingMessage>((observer, cancel) =>
            {
                var url = type == StreamingType.User ? "https://userstream.twitter.com/1.1/user.json" :
                          type == StreamingType.Site ? " https://sitestream.twitter.com/1.1/site.json " :
                          type == StreamingType.Filter ? "https://stream.twitter.com/1.1/statuses/filter.json" :
                          type == StreamingType.Sample ? "https://stream.twitter.com/1.1/statuses/sample.json" :
                          type == StreamingType.Firehose ? "https://stream.twitter.com/1.1/statuses/firehose.json" : "";

                return e.IncludedTokens.SendStreamingRequestAsync(type == StreamingType.Filter ? MethodType.Post : MethodType.Get, url, parameters.Parameters, cancel)
                    .ContinueWith(task =>
                    {
                        if(task.IsFaulted)
                            throw task.Exception.InnerException;

                        return task.Result.GetResponseStreamAsync();
                    }, cancel)
                    .Unwrap()
                    .ContinueWith(task =>
                    {
                        if(task.IsFaulted)
                            throw task.Exception.InnerException;

                        using(var reader = new StreamReader(task.Result))
                        using(var reg = cancel.Register(() => reader.Dispose()))
                        {
                            foreach(var s in reader.EnumerateLines().Where(x => !string.IsNullOrEmpty(x)))
                            {
                                observer.OnNext(RawJsonMessage.Create(e.IncludedTokens, s));
            #if !DEBUG
                                try
                                {
            #endif
                                observer.OnNext(StreamingMessage.Parse(e.IncludedTokens, s));
            #if !DEBUG
                                }
                                catch { }
            #endif
                            }
                        }
                    }, cancel, TaskContinuationOptions.LongRunning, TaskScheduler.Default)
                    .ContinueWith(task =>
                    {
                        if(task.IsFaulted)
                            throw task.Exception.InnerException;
                    }, cancel);
            });
        }
开发者ID:kb10uy,项目名称:CoreTweet,代码行数:59,代码来源:Stream.cs

示例2: Fetch

 internal static IEnumerable<string[]> Fetch(string uri)
 {
     string[] res;
     using(var x = new StreamReader(WebRequest.Create(uri).GetResponse().GetResponseStream()))
         res = x.EnumerateLines().ToArray();
     var s = new List<string>();
     foreach(var x in res)
     {
         if(x.Equals("----") || x.Equals(res.Last()))
         {
             yield return s.ToArray();
             s.Clear();
         }
         else
             s.Add(x);
     }
 }
开发者ID:cannorin,项目名称:SecondSympathy,代码行数:17,代码来源:Tools.cs

示例3: AccessStreamingApiAsObservableImpl

        private static IObservable<StreamingMessage> AccessStreamingApiAsObservableImpl(StreamingApi e, StreamingType type, IEnumerable<KeyValuePair<string, object>> parameters)
        {
            return Observable.Create<StreamingMessage>((observer, cancel) =>
            {
                return e.IncludedTokens.SendStreamingRequestAsync(GetMethodType(type), e.GetUrl(type), parameters, cancel)
                    .ContinueWith(task =>
                    {
                        if(task.IsFaulted)
                            task.Exception.InnerException.Rethrow();

                        return task.Result.GetResponseStreamAsync();
                    }, cancel)
                    .Unwrap()
                    .ContinueWith(task =>
                    {
                        if(task.IsFaulted)
                            task.Exception.InnerException.Rethrow();

                        try
                        {
                            using (var reader = new StreamReader(task.Result))
                            using (cancel.Register(() => reader.Dispose()))
                            {
                                foreach (var s in reader.EnumerateLines().Where(x => !string.IsNullOrEmpty(x)))
                                {
                                    try
                                    {
                                        observer.OnNext(StreamingMessage.Parse(s));
                                    }
                                    catch (ParsingException ex)
                                    {
                                        observer.OnNext(RawJsonMessage.Create(s, ex));
                                    }
                                }
                            }
                        }
                        finally
                        {
                            cancel.ThrowIfCancellationRequested();
                        }
                    }, cancel, TaskContinuationOptions.LongRunning, TaskScheduler.Default);
            });
        }
开发者ID:aktaat,项目名称:CoreTweet,代码行数:43,代码来源:Stream.cs

示例4: ThreadMain

		void ThreadMain(object state)
		{
			var auth = (OAuthAuthorization)state;

			this.Account = auth.Token;
			this.Terminated = false;

			try
			{
				var ub = new UriBuilder(TwitterUriBuilder.Stream.User(this.Track, this.Follows));
				var query = string.IsNullOrEmpty(ub.Query) ? null : ub.Query.TrimStart('?');

				ub.Query = null;

				using (var wc = new CustomWebClient
				{
					Headers =
					{
						{ HttpRequestHeader.UserAgent, "Solar/" + Assembly.GetEntryAssembly().GetName().Version },
					},
				})
				using (var ns = wc.OpenPost(ub.Uri, (string.IsNullOrEmpty(query) ? null : query + "&") + auth.CreateParameters("POST", ub.Uri, query)))
				using (var sr = new StreamReader(ns))
				{
					Connected.RaiseEvent(this, EventArgs.Empty);

					try
					{
						foreach (var i in sr.EnumerateLines()
											.Where(_ => !string.IsNullOrEmpty(_))
											.Select(DynamicJson.Parse))
						{
							if (IsDelete(i))
							{
								// delete
								if (i.delete.status())
									DeleteStatus.RaiseEvent(this, new EventArgs<StatusID>(i.delete.status.id));
							}
							else if (IsFriends(i))
							{
								// friends
								Friends.RaiseEvent(this, new EventArgs<IList<UserID>>(((dynamic[])i.friends).Select(_ => (UserID)_).Freeze()));
							}
							else if (IsEvent(i))
							{
								// event
								using (var client = new TwitterClient(auth.Token, StatusCache))
								{
									var e = new TwitterStreamEventArgs(client, i);

									switch (e.Type)
									{
										case "follow":
											if (e.Source.UserID == this.Account.UserID)
												Follow.RaiseEvent(this, e);
											else
												Followed.RaiseEvent(this, e);

											break;
										case "unfollow":
											Unfollow.RaiseEvent(this, e);

											break;
										case "block":
											Block.RaiseEvent(this, e);

											break;
										case "unblock":
											Unblock.RaiseEvent(this, e);

											break;
										case "favorite":
											if (e.Source.UserID == this.Account.UserID)
												Favorite.RaiseEvent(this, e);
											else
												Favorited.RaiseEvent(this, e);

											break;
										case "unfavorite":
											if (e.Source.UserID == this.Account.UserID)
												Unfavorite.RaiseEvent(this, e);
											else
												Unfavorited.RaiseEvent(this, e);

											break;
										case "retweet":
											if (e.Source.UserID == this.Account.UserID)
												Retweet.RaiseEvent(this, e);
											else
												Retweeted.RaiseEvent(this, e);

											break;
									}
								}
							}
							else if (IsDirectMessage(i))
							{
								// direct message
								using (var client = new TwitterClient(auth.Token, StatusCache))
									DirectMessage.RaiseEvent(this, new EventArgs<DirectMessage>(new DirectMessage(client, i.direct_message)));
//.........这里部分代码省略.........
开发者ID:karno,项目名称:OpenSolar,代码行数:101,代码来源:TwitterStream.cs


注:本文中的System.IO.StreamReader.EnumerateLines方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。