Basically, I have a form with a button, when the button is pressed, it creates an instance of the class that runs Thread. When a thread executes, it automatically calls Thread.Abort ().
The code I have comes down to the following:
Button
private void Buttonclick(object sender, EventArgs e) { MyClass c = new MyClass() c.Do_your_thing(); }
Grade:
public class MyClass { Thread t; public void Do_your_thing() { t = new Thread(Running_code); t.Start(); } private void Running_code() {
When I press the button once, everything works. But when I press the button again, nothing happens.
When I do not use t.Abort (), everything works. But not using t.Abort () will cause a memory leak, and the program will not close properly (the thread never closes, so the process will remain alive).
Can someone explain to me what is going on? And how can I fix this?
EDIT: upon request I send some actual code
public class MyClass { public void Test() { t = new Thread(() => { wb.DocumentCompleted += get_part; wb.Navigate("http://www.google.com"); Application.Run(); }); t.SetApartmentState(ApartmentState.STA); t.Start(); } public void get_part(object sender, WebBrowserDocumentCompletedEventArgs e) { var br = sender as WebBrowser; string url = e.Url.ToString();
This is a small part of the small webscraper program. It moves to a web page that requires some login information. When I reach the page that I really want to be on, he should open it in the new Internet Explorer.
When I call this code and close the form, it is still displayed in the process tree. And when I press the button several times, the used memory continues to grow, and I suspect that this is some kind of memory leak.