Any time, but because registering a copyright costs money ($35 in the US, $65 in Canada), most people never do it, and those who do will generally register a copyright after the work is in its final, publication form - often after publication.
Registering a copyright does not increase your work’s protection under the law. What it does do is make it easier to pursue a claim against an infringer in court, and generally get higher compensation if you win. For example, I think that in the US, if you win - and winning is more likely if you have registered - the infringer would automatically have to cover court costs, and the penalty might be doubled. So, in the US, copyright registration gives your claim more teeth. In Canada, it’s less clear that it does anything. But it doesn’t make the material any more ‘yours’ than it was the instant you wrote it.
There are some practical considerations. Many filings are by commercial outfits (including publishers) that believe that having to defend their claim is a realistic prospect. For example, material for advertising and promotion, or music that tends to get ripped off. For an individual, while filing might give you comfort, it may have limited practical value. Copyright registration is not the same as ‘title’ (i.e. evidence of ownership). It is just a public proclamation of ownership. Anyone can publicly claim to own your work - that doesn’t make it theirs in law. Taking an infringer to court is a slow and costly process, and even if you have registered your copyright, you still might have to bear the cost yourself. Many, many infringers do their infringement in jurisdictions (e.g Asian countries) where you have no effective way of suing them; they’ll just ignore you. A better path is to demand that commercial portals that distribute infringing work (e.g. Wattpad, which does sometimes inadvertently disseminate infringing works) take it down. Usually, they will.
Here’s a FAQ for US copyright protection and registration and a FAQ for Canadian copyright protection and registration.
Here’s a legal blog article about the benefits of registering in Canada; it mentions a representative case.
Someone mentioned the DMCA. That’s a set of modernization rules that, for the most part, don’t help you, the copyright holder, but rather protect Internet-based service providers like Wattpad from being sued when others use them to commit copyright violations.
I’m not a lawyer. Each person should do their own research and come to their own conclusions. Or consult a lawyer