下面的代码按预期工作,但我不确定我会使用最优雅的代码。
using (DatabaseContext context = DatabaseContext.CreateContext(_incompleteConnString + prefix + campaignDBPlatform))
Progress prog = new Progress();
TaskFactory tf = new TaskFactory();
var parent = tf.StartNew(() =>
Parallel.ForEach(QuestionsLangConstants.questionLangs.Values, i =>
{
try
{
qrepo.UploadQuestions(QWorkBook.Worksheets[i.QSheet], i, prog);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
})
);
prog.Show();
var finalTask = parent.ContinueWith(i =>
{
using (DatabaseContext context2 = DatabaseContext.CreateContext(_incompleteConnString + prefix + campaignDBPlatform))
{
UploadedQuestionsRepliesRepository uqrepo = new
UploadedQuestionsRepliesRepository(context2);
UploadedQuestionsReplies UQuestions = new UploadedQuestionsReplies() {
Id = (int)uqrepo.getNextSeqValue(),
FileName = "test",
RQType = Questions.QuestionsType.ToString(),
TimeStamp = DateTime.Now
};
uqrepo.Insert(UQuestions);
uqrepo.Save();
}
});
}
如果我不添加context2
context
,当我在父母继续时会被处置。但是,如果我使用 a finalTask.Wait()
,则 UI 会冻结。对我上面的内容有更好的解决方案吗?