本文整理汇总了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);
});
}
示例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);
}
}
示例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);
});
}
示例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)));
//.........这里部分代码省略.........