【发布时间】:2018-05-02 21:39:35
【问题描述】:
我正在构建一个应用程序,它将连接到 Twitter,接收推文数据并将它们存储到数据库中。之后,应用程序将从数据库中检索数据,分析内容,如果有与另一个预设数据库匹配的内容,则会弹出 toast 通知。
总共将有 5 个任务需要同时运行。
Task 1 => GetKeyword 方法(从数据库中获取关键字列表)
任务 2 => Connect 方法(连接到 Twitter 并通过关键字过滤流数据并存储到数据库中)
任务 3 => RetrieveData 方法(从数据库中检索存储的流数据)
任务 4 => 分析方法(分析推文内容并找到匹配的内容)
Task 5 => Notify 方法(如果匹配,通过弹出 toast 通知通知用户)
所以以前我设法让它们完全运行,但是使用这些代码有时它会永远停留在Connect 任务,并且不会继续执行RetrieveData 任务和Analyze 任务。
如何限制Connect 任务的运行时间并让它在RetrieveData 任务和Analyze 任务上继续运行?我已经限制了存储在方法本身内的流数据的数量,但似乎只停止流本身而不是线程。
这是关于多线程的代码:
List<string> streamdata = new List<string>();
List<string> keyList = new List<string>();
try
{
var task = Task.Run(() => GetKeyword(0))
.ContinueWith(prevTask => Connecting(1000, keyList))
.ContinueWith(prevTask => RetrieveData(1500))
.ContinueWith(prevTask => MakeRequest(2000, streamdata))
.ContinueWith(prevTask => Notify(2500, cyberbully, notification));
task.Wait();
}
catch (Exception ex)
{
MessageDialog messagebox = new MessageDialog("Task running error:" + ex);
await messagebox.ShowAsync();
}
这是连接方法代码:
public static void Connecting(int sleepTime, List<string> keyList)
{
//Set the token that provided by Twitter to gain authorized access into Twitter database
Auth.SetUserCredentials("YTNuoC9rrJs8g9kZ0hRweKrpp", "wXj6VSl68jeFStRWHDnhG19oP1WZGeBFMNgT3KCkI6MaX46SMT", "892680922322960384-8ka1NuhgiuxjSLUffQVdwmnOIbIduZa", "y92ycGrGCJS9vBJU79gq34rV6FCwNjBPFFOqhEHaTQe1l");
//Create stream with filter stream type
var stream = Stream.CreateFilteredStream();
int numoftweet = 0;
//Set language filter to English only
stream.AddTweetLanguageFilter(LanguageFilter.English);
//Connect to database that stored the keyword
foreach (var key in keyList)
{
stream.AddTrack(key);
}
//Let the stream match with all the conditions stated above
stream.MatchingTweetReceived += async (sender, argument) =>
{
//Connect to MongoDB server and database
var tweet = argument.Tweet;
try
{
var client = new MongoClient();
var database = client.GetDatabase("StreamData");
var collection = database.GetCollection<BsonDocument>("StreamData");
//Exclude any Retweeted Tweets
if (tweet.IsRetweet) return;
//Store the data as a BsonDocument into MongoDB database
var tweetdata = new BsonDocument
{
//Store only the data that needed from a Tweet
{"Timestamp", tweet.TweetLocalCreationDate},
{"TweetID", tweet.IdStr},
{"TweetContent",tweet.Text},
{"DateCreated", tweet.CreatedBy.CreatedAt.Date},
{"UserID", tweet.CreatedBy.IdStr},
{"Username", tweet.CreatedBy.Name}
};
//Insert data into MongoDB database
await collection.InsertOneAsync(tweetdata);
//Every tweets streamed, add 1 into the variable
numoftweet += 1;
//If the number of tweets exceed 100, stopped the stream
if (numoftweet >= 100)
{
stream.StopStream();
}
}
//Catch if any exception/errors occured
catch (Exception ex)
{
MessageDialog messagebox = new MessageDialog("MongoDB Connection Error:" + ex);
await messagebox.ShowAsync();
}
};
//Start the stream
stream.StartStreamMatchingAllConditions();
}
备注:这是一个 UWP 应用程序,此代码位于按钮后面。
【问题讨论】:
-
这五个任务不会“同时”运行(它们也不能,因为它们每个都依赖于前一个任务的结果)。由于您的任务本身可能是异步的(网络调用、数据库查找等),您应该更仔细地研究如何使用
async/await-- 可能从一个更简单的示例问题开始。 -
@PeterTorr-MSFT 是的,这些方法是异步的。我明白了,所以我应该使用 async/await 来运行它们而不是使用 Task.Run()?
-
是的。您可以只在每次通话时
await,一个接一个。 -
@PeterTorr-MSFT 谢谢!!
标签: c# multithreading uwp async-await task-parallel-library