It actually bit me a couple of times. If you are doing simple code as follows:
private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < 1000000; i++) { Thread CE = new Thread(SendCEcho); CE.Priority = ThreadPriority.Normal; CE.IsBackground = true; CE.Start(); Thread.Sleep(500); GC.Collect(); } } private void SendCEcho() { int Counter = 0; for (int i = 0; i < 5; i++) { Counter++; Thread.Sleep(25); } }
Run this code and see how the pens fly! Thread.Sleep is that you can close it and not take over the computer. This should ensure that the thread starts before the start of the next thread. Call GC.Collect (); doing nothing. From my observation, this code loses 10 descriptors of each update in the task manager during a normal update.
It doesn't matter what is in the void SendCEcho () function, if you want, count to five. When the thread dies, there is one handle that does not peel.
In most programs, this does not matter much because they do not work for long periods of time. In some programs that I created, they should run within a few months and months.
If you use this code again and again, you can eventually test the descriptors until the time that Windows becomes unstable and will not work. Reboot required.
I solved the problem using a thread pool and code like this:
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadJobMoveStudy));
My question, however, is why there is such a leak in .Net and why does it exist for so long? So how is 1.0? I could not find the answer here.
source share