This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21695|Reply: 1

[Source] .net/c# Be careful of events that drag down your program

[Copy link]
Posted on 9/21/2016 4:05:58 PM | | | |
Events are loaded and run on the main thread, and if the first event on the main thread is not finished, you come to the second event, just like other programs in the main thread, wait for the previous one to finish processing before processing the next or other program or event in the main thread.




The above is, I encapsulated an object myself, and in the object, I encapsulated an event myself.

I process data in the method of subscribing to events, such as the image annotation part,

Because the event I subscribed to was a read txt file, and my txt file had 50,000 lines, causing the event method to be triggered 50,000 times.

Then, when I ran the program, I found that my interface was in a state of suspended animation, and I knew that there must be a problem there.

I initially thought it was the manipulation of the UI controls in the method that caused fake death.

Then, step by step, it was found that when adding data to the set, it had already entered a state of suspended animation.

why??? Finally, I found out from the internet that the event is on the main thread, and if the first event is not processed, it will block the execution of the next event,

I generally understood what I ordered, to put it bluntly, 50,000 events caused the program to be blocked, and then I entered a state of suspended animation.

Solution:

For some simple events, which will not lead to the execution of a large number of event methods, can be written directly into the event method.

For a large number of event methods called, I hope you will open a thread to deal with it, such as: socket or httplistener, etc. (the amount of data is small and you can't see it, once the amount of data is large, haha, it will die directly)




Previous:C# generates email address codes in bulk
Next:Cloud broadcast player analysis, Thunderbolt and QQ whirlwind interface analysis
Posted on 9/22/2016 9:23:17 AM |
Someone posted very well, support
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com