How to control the status of BeginInvoke?
I have a function as below.
private void AddPrice(String value)
{
Delegate del=new Action<String>(AddToCollection);
this.Dispatcher.BeginInvoke(del,DipatcherPriority.Background,value);
}
In AddToCollection method, the value will be added to Observable Collection.
"开发者_运维知识库AddPrice" function will be called based on user input(Eg. 100 times, 200 times, 300 times).
the maximum count of Observable collection is 150. If it reaches 150, I have to pop up Messagebox to user and need to cancle adding values. But, If the function is called 200 times, the messagebox is showing 50 times bse of BeginInvoke. How can I modify this? thanks.
You have two options:
- Set some flag once user cancels adding values, and ignore AddToCollection calls after this.
- Replace BeginInvoke with Invoke and return false when adding is canceled. Stop calling Invoke when it returns false.
So, you can stop this process on receiver or on the sender side.
精彩评论