Click here to Skip to main content
15,897,160 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
VB
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?
Posted

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.
 
Share this answer
 
Comments
Mr.Monto2000 12-Dec-15 11:26am    
The problem is not about how many buttons or tools I want to create. The problem is about how can I create them from abackgroundWorker. because I made a very huge operation, And I want the user to can cancel it.
Dave Kreskowiak 12-Dec-15 13:21pm    
You don't get it. Yes, you're solving this problem, but your design is creating another one that you can't code your way out of.

You can't put hundreds of controls on a form and expect it to render without any problems. You WILL have performance issues.
(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:
VB
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:
VB
Function Invoke_CreateTool2(Container As StackPanel) As Integer
    Return Container.Dispatcher.Invoke(Function()
                                           Return Container.Children.Add(New Button())
                                       End Function)
End Function
 
Share this answer
 
Comments
Mr.Monto2000 12-Dec-15 11:24am    
First, Thank you for replying. Second, I tried your code. Same problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900