Home > Net >  Singleton mode how to cancel the registration of event
Singleton mode how to cancel the registration of event

Time:11-10

I made A serial of DLL, and then the singleton instance, to cope with the situation of calls between different pages, but the event registration for receiving data processing is on each page, as A result, A registration page, open the page, B B page registered again, had two incident response, B page have different treatment, must want to register, so I also don't want to do in each page event cancellation, excuse me is there any way to write the event of cancellation in singleton? Or other solutions, but had better not write separately for each page

CodePudding user response:

Why should such a design? Since design events, then the caller register event (subscription) will inevitably involve the corresponding cancellation, because be registered party cannot know when should be cancelled,

CodePudding user response:

The move chopsticks have to move chopsticks, nobody will give it to you

Even if the ans packing become

Using (visit xx=new wrapper agents to access the wrapper (Action xx))
{
}

Also have to have to write a wrapper, oneself in the Disposable method -=his

Open in said, A, B open this description, you feel how do we know that B is opened, A don't have?? And then I switch to A, B don't have??

If so, the wrapper with a activation action,

Question, however, I don't want to write -=, so we say that you even don't want to write the activation, so basically no solution, is we let the computer action, not the computer was fed to you

CodePudding user response:

I message processing interface tagetobject {get; The set; }


Your serial message arrival
{
Tagetobject? Invoke (message)
}


Active action, of course, to activate tagetobject assignment, you have to write, of course it is a practical writing, anyway you ask is it a goal to do at the same time, this meets your requirements

CodePudding user response:

Just read a book "" mentioned in the concept of weak entrust, can refer to:

In programming for the head, if an event registration are registered for an event with the event publisher, publisher will save a strong reference for the registered person, if the event registration is not properly logout events, so the publisher commissioned chain has been in the table contains a strong reference to the registrar, as a result, the registration in the heap memory will never be the CLR recycling, if such registration belongs to large objects or numerous, very light yi will be made out of memory in the piles, weak entrust is able to solve this problem, as long as we will be used in the event programming is entrusted with weak,

Weeks see wisdom; Botu hin. Uniting the way: the.net development earnestly points (Kindle location 2136-2140). Tsinghua university press. The Kindle version.

CodePudding user response:

First use "normal thinking logic" to write the program, don't around the technology too much things to gild the lily, if no need not to introduce technology,

Your life span period of objects, their own reference registration to events, life span is very long objects you closing the short-term objects, why didn't you logout event listeners when call related close method? Makes no sense,

CodePudding user response:

The
refer to the original poster doyouknowm response:
excuse me is there any way to write the event of cancellation in singleton? Or other solutions


Your event handler method is each category page inside the code, what is called "wrote in a singleton"? If head are misplaced, so there would be no shape,

CodePudding user response:

Don't want to do so, in addition to do a registered function, add a parameter reference page, the page mapping from function, check each register event triggering event corresponding page is still valid

CodePudding user response:

Educated, thank you
  •  Tags:  
  • C#
  • Related