0

I'm using Indy 10 in Delphi 7. I have a TidCookieManager in a main form and I wish to copy its cookies over to another cookie manager in a separate thread, this thread obviously has access to the main form.

How do I copy those cookies?

Thanks,

David

rave154
  • 11
  • 1
  • 1
    Start by taking a look at TIdCookieManager's CookieCollection and CopyCookie(). Run the code in a synchronize block. Take a look at the answer to: [Save Cookies inside TIDCookieManager to file](https://stackoverflow.com/questions/14382732/save-cookies-inside-tidcookiemanager-to-file) for some help with interating through cookies. – Brian Sep 18 '19 at 12:27
  • 1
    "*Run the code in a synchronize block*" - that is not necessary since the `CookieCollection` uses a thread lock (specifically, a `TMultiReadExclusiveWriteSynchronizer`). Locking the source and destination `CookieCollection`s while iterating+copying will provide adequate synchronization. Also, you don't need to iterate+copy the individual cookies manually as the `CookieCollection` has `Assign()` and `AddCookies()` methods to handle that operation for you. – Remy Lebeau Sep 18 '19 at 17:08

1 Answers1

3

TIdCookieManager has a public CookieCollection property of type TIdCookies which provides access to the actual cookies. The cookies of one TIdCookies can be directly copied to another TIdCookies via its Assign() or AddCookies() method, eg:

// clears the dest collection before then copying cookies to it...
CookieMgrInWorkerThread.CookieCollection.Assign(CookieMgrInMainThread.CookieCollection);
// does not clear the dest collection before copying cookies to it...
CookieMgrInWorkerThread.CookieCollection.AddCookies(CookieMgrInMainThread.CookieCollection);

Either way, TIdCookies is thread-safe, as it uses an internal TMultiReadExclusiveWriteSynchronizer during read/write operations.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770