Tuesday, October 11, 2011

Thread Pooling work items in C#

Sometimes you want to perform a work item in another thread but not go through the hassle to create a new thread. A good way to solve the problem is to use the ThreadPool.
using System.Threading;
namespace dsc.tools
    public class ThreadPooling
        public void StartWorkItem()
            ThreadPool.QueueUserWorkItem(context =>
                // do stuff here
            // or
        private void DoWork(object context)
            // do stuff here

Monday, June 13, 2011

MongoDB C# Driver, Querying multiple fields with intervals

Simple way of chaining together a query when looking for records that require both field one and field two to be in a certain interval.
double foo = 1.0;
double bar = 2.0;
MongoCollection<BsonDocument> items = db.GetCollection<BsonDocument>("collection");
var query = Query.And(
    (Query.GT("field_one", foo).LT(bar)),
    (Query.GT("field_two", foo).LT(bar))
MongoCursor<BsonDocument> dbResult = items.Find(query)

Thursday, June 9, 2011

MongoDB C# Driver, Query multiple tags that are required

Tried hard to find the solution to querying multiple tags through the MongoDB C# driver.
Many of the solutions that I found were about using OR to put together the tags but I needed to AND the tags. I.e. all tags supplied were required to be present on in the result.
The solution was quite simple actually. Just my mind that thought it would require some special solution.
string[] tags = new string[] {"foo", "bar"};
var sort = SortBy.Descending("created");
var query = MongoDB.Driver.Builders.Query.All("tags", BsonArray.Create(tags));
var dbPages = pages.Find(query)

  1. Create a string array with the tags that you want to query with
  2. Add sorting if needed
  3. Call MongoDB.Driver.Builders.Query to build your query
  4. Use find on your MongoCollection together with your query.
  5. Add Limit and Offset if needed.
Enjoy : )

SGEN: Could not load file or assembly 'xxx' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

Always nice to see our old friend HRESULT in an error message.

Ran into this today when converting some .NET3.5 projects to .NET4.0 and at the same time upgrading some 3rd party libraries.

Seems that downloaded files get a blocked attribute set on them that prevents the usage of them. In Visual Studio 2010, the HRESULT: 0x80131515 is shown when linking to dll’s that are blocked.

To come around this, open Windows Explorer, navigate to the folder hosting the dll. Right click on it and select properties. There should be a Unblock button in the dialog window. Click on it and SGEN should stop complaining.

Hope this helps : )