Imagine that I have the following two methods:
void AddEvenNumbers()
{
for(int i=0; i<10000000; i+=2)
{
listbox1.items.add(i); 'just coded by hand, did not really test
}
}
void AddOddNumbers()
{
for(int i=1; i<10000000; i+=2)
{
listbox1.items.add(i); 'just coded by hand, did not really test
}
}
I call those methods (in Windows Forms) in a synchronized manner (which works fine):
void button_click(...)
{
AddEvenNumbers();
AddOddNumbers();
}
How can I use TPL (parallel library) to achieve the same (not worried about the order of elements being added to listbox) in a best possible manner with respect to the following?
- the app should not behave as “not responding”
- listbox needs to be updated/refreshed (instantaneously) when the items are being added (not when the whole process is completed).
I am in the process of learning TPL and trying to apply the concepts using WinForms (with very least success rate). I tried several ways and not successful.
Also, are there any fundamental things I should consider before applying TPL to WinForms (just wanted to make sure that I am not missing anything).
Highly appreciate your time on this.
This is not possible.
You can only interact with the UI on the UI thread.
However, you can use the TPL to create the set of items you want to add if they take a long time to create (eg, if they come from a DB or website)