BackgroundWorker&创建新的UI [英] BackgroundWorker & create new UI

查看:110
本文介绍了BackgroundWorker&创建新的UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个长线程女巫在我的窗口上创建了一些新的控件。如何使用backgroundWorker执行此操作以使用进度条和取消功能。

我使用此代码创建新控件。它工作正常,但我有一个错误。

我的代码是

 功能 Invoke_CreateTool(Container  As  StackPanel) As   Integer  
Dim cntrl = Application.Current.Dispatcher.Invoke( New Func( 按钮)
功能() Button()))
Dim Int = Container .Dispatcher.Invoke( New Func( 整数)(功能 ()Container .Children.Add(cntrl)))
返回 Int
结束 乐趣ction





错误参数计数不匹配

我该怎么办?

解决方案

你创建了多少个按钮?如果您需要创建一个进度条来创建按钮,那么问题就是创建按钮的时间长度。这将是减少你的应用程序的按钮数量。


(警告:我主要是ac#开发人员。我的VB应被视为怀疑!)

我将你的代码粘贴到VB中并删除了显式委托创建,并让重载决策找出lambdas的类型:

 功能 Invoke_CreateTool(Container  As  StackPanel) As  整数 
Dim cntrl = Application.Current.Dispatcher.Invoke(函数()按钮())
Dim Int = Container.Dispatcher.Invoke( Function ()Container.Children.Add(cntrl))
返回 Int
结束 功能



VB对此感到满意(至少在编译时)。

但是,你为什么要做两个调用 s?在同一个 Dispatcher 上创建按钮是否更有意义容器使用,因为那肯定是UI上下文?例如:

 功能 Invoke_CreateTool2(容器 As  StackPanel)作为 整数 
返回 Container.Dispatcher.Invoke( Function ()
返回 Container.Children.Add( 按钮())
结束 功能
结束 功能


I am running a long thread witch creates some new controls on my window. How can I do this with a backgroundWorker to use a progress bar and cancellation capabilities.
I used this code to Create the new controls. It works fine, But I've got an error.
My code is

Function Invoke_CreateTool(Container As StackPanel) As Integer
  Dim cntrl = Application.Current.Dispatcher.Invoke(New Func(Of Button)
  (Function() New Button()))
  Dim Int = Container .Dispatcher.Invoke(New Func(Of Integer)(Function() Container                        .Children.Add(cntrl)))
    Return Int
End Function



And the error is parameter count mismatch
What shall I do?

解决方案

How many buttons are you creating? If you need to create a progress bar to create buttons, you're problem is going to be the length of time to create the buttons. It's going to be the number of buttons that's going to slow your application down.


(Caveat: I'm primarily a c# developer. My VB should be considered suspect!)
I pasted your code into VB and removed the explicit delegate creation and let the overload resolution figure out the types for the lambdas:

Function Invoke_CreateTool(Container As StackPanel) As Integer
    Dim cntrl = Application.Current.Dispatcher.Invoke(Function() New Button())
    Dim Int = Container.Dispatcher.Invoke(Function() Container.Children.Add(cntrl))
    Return Int
End Function


VB seems happy with this (at least at compile time).
However, Why are you doing two Invokes? Wouldn't it make more sense to create the Button on the same Dispatcher that the Container uses, since that will certainly be the UI context? Like:

Function Invoke_CreateTool2(Container As StackPanel) As Integer
    Return Container.Dispatcher.Invoke(Function()
                                           Return Container.Children.Add(New Button())
                                       End Function)
End Function


这篇关于BackgroundWorker&创建新的UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆