Page 1 of 1

Tokens and Threads

Posted: Mon Aug 17, 2009 10:23 pm
by gunter
G'day guys,

I have a couple of worker functions that need to run in the background, so that the page isn't being held up (and I'm using a polled AJAX update panel).
I have considered using the BackgroundWorker class, but would like this to works as I have alot of code that is threaded in a classic manner.
The issue is that one of the worker functions, which deletes products, categories, manufacturers and vendors, when in another managed thread, doesn't work.

The function uses a simple:

Code: Select all

foreach (Product prod in Token.Instance.Store.Products)
            {
                ++prodCount;
                if (prod != null)
                    prod.Delete();
            }
And the other foreach loops for the categories, manufacturers and vendors.

This function works if it is not threaded, that is, if it's in the code behind the button.
The Token.Instance.Store.Products collection is seemingly empty.

I have tried the following code before the foreach statements:

Code: Select all

Token.Instance.Store = new Store(StoreID);
Where the StoreID is stored in a static int, which is equalling the correct value (this I have checked),
But it still doesn't delete the items.

Any help is greatly appreciated,
The wall I'm banging my head against is getting sick of me.

Re: Tokens and Threads

Posted: Tue Aug 18, 2009 8:54 pm
by AbleMods
I'm not sure if I follow you completely, but I think I know the answer.

In 7.0.2(maybe 7.0.3 not sure which), Able stopped populating the Token instance when HTTP context was not established. In other words, a separate thread without an HTTP context won't automatically have the Token instance populated like you would expect.

The workaround (for me at least) was easy - just call:

Code: Select all

Token.Instance.InitStoreContext(StoreDataSource.Load(1))

Re: Tokens and Threads

Posted: Thu Aug 20, 2009 4:14 pm
by Logan Rhodehamel
Joe is right... it was 7.0.2. We had to stop assuming that your store ID was 1. In a non HTTP thread, you need to tell the token which store to work with.

Re: Tokens and Threads

Posted: Thu Aug 27, 2009 6:37 am
by gunter
Thanks guys,

I managed to find that code in another of our sites' modules (which I had written and forgotten about).

Very useful peice of information, should be in the Wiki.