关于多线程传递参数调用方法
学习完多线程通信方法,界面再也不会卡死了,但是项目中有级联反应,需要传递参数调度线程。
一级级联:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(new ThreadStart(GetProjectList));
thread.IsBackground = true;
thread.Start();
}
private void GetProjectList()
{
//Raw data
ProjectList = GetProject();
//从主 UI 线程派生的后台线程不能更新的内容 Button UI 线程上创建。 为了使后台线程访问的内容属性的 Button, ,后台线程必须将工作委托给 Dispatcher 与 UI 线程关联
Action action = () => { cbProject.ItemsSource = ProjectList; cbProject.SelectedIndex = 0; };
if (System.Threading.Thread.CurrentThread != cbProject.Dispatcher.Thread)
{
cbProject.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, action);
}
else
{
action();
}
}
二级级联:
private void cbProject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string Project = cbProject.SelectedValue.ToString();
if(string.IsNullOrEmpty(Project) || Project.Equals("Project"))
{
return;
}
Thread thread = new Thread(new ParameterizedThreadStart(GetWaferNoList));
thread.IsBackground = true;
thread.Start(Project);
}
(参数类型需要object)
private void GetWaferNoList(object strProj)
{
WaferList=GetWaferList();
Action action = () => { cbWaferNo.ItemsSource = WaferList; cbWaferNo.SelectedIndex = 0; cbWaferNo.IsEnabled = true;
lblWafer.Content = string.Empty; btnGenerate.IsEnabled = true; cbProject.IsEnabled = true; };
if (System.Threading.Thread.CurrentThread != cbWaferNo.Dispatcher.Thread)
{
cbWaferNo.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, action);
}
else
{
action();
}
}