[英]Run a method after finish another one in C#?
我猜想,我在C#/ WPF中進行線程處理有問題。
為了防止諸如Null Value之類的問題,如何接連運行一種方法。
我認為下面的代碼“ IF語句”在“ selectLessonContent(selectedLessonId)”方法之前運行,並在下面導致空值問題。
當我在selectLessonContent(selectedLessonId)方法之后立即使用MessageBox.Show()時,此問題消失了。
private void btnSelectLesson_Click(object sender, ExecutedRoutedEventArgs e)
{
selectLessonContent(selectedLessonId);
if (selectedUserID != "QuickStart")
{
int lessonScore = Int32.Parse(userlessonManager1.selectUserLesson("existCheck", selectedUserID, selectedLessonId).Rows[0][3].ToString());
if (lessonScore < sectionListview.Items.Count )
{
for (int a = 1; a < sectionListview.Items.Count; a++)
{
ListViewItem item = sectionListview.ItemContainerGenerator.ContainerFromIndex(a) as ListViewItem;
ContentPresenter templateParent = GetFrameworkElementByName<ContentPresenter>(item);
DataTemplate dataTemplate = sectionListview.ItemTemplate;
//error occured Here : Value cannot be null.
Button btnTemp = dataTemplate.FindName("btnSectionList", templateParent) as Button;
btnTemp.IsEnabled = false;
}
}
}
}
最簡單的解決方案是將所有異步代碼包裝到一個方法中,因此它將在另一個線程中同步運行。 但是您必須小心從另一個線程訪問GUI
如果您需要更復雜的解決方案,則可以使用async / await,這可能是WF / WPF的最佳選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.