开发者

Abort threads associated with Unreferenced Objects

I am working on a form in this form when user clicks a button a new thread is generated that Created a new form and do some work and show the result. See thee following code I have 开发者_如何学JAVAmade the main class in my project that stores the references of each type of object

Program.someFormReference

When user click the button a new thread creates and Program.someFormReference=new CreatedForm(). Now what will be if user presses button more than once.We have more than one objects in memory with threads working on them. But I had reference to only one thread. One solution to avoid this situation is to make button disable until thread completes it,s works. What other solution can be to avoid this uncontrolled thread chain.


What you should do first is to decide on the following question:

Do you want to have one or many such threads active?

It seems you are undecided on that issue so figure that one out first.

Then, if: I want to have only one thread active, then you either:

  1. Disallow starting another thread until the first one is finished
  2. Stop the active one before starting a new

To stop the active one, if it is running a form, simply close that form. ie. if you have this in your thread:

Application.Run(someform);

then close that form. You need to use someform.Invoke because it is running on another thread but that should work just as you want.

Or, if: I want to have many such threads active, then you need to keep a list of active threads around, and manage this list, ie. remove entries when threads finish, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜