edit: in case someone is asking a question, the actionhandler calls the code that creates and uses the same type of datacontext, in case this may have anything to do with this behavior. the code does not apply to the MatchUpdateQueue table, but I think I should mention this just in case.
double edit: everyone who answered was right! I answered the defendant, who underwent most of my survey. fixing the problem allowed another problem to emerge (hidden inside the handler) that caused the same exception. oops!
I am having trouble deleting items in LINQ. Calling DeleteOnSubmit in the code below throws a LINQ exception with the message "Unable to add an object with a key that is already in use." I'm not sure what I'm doing wrong here, he starts pushing me against the wall. The primary key is just an integer auto-increment column, and I have no other problems until I try to remove the item from the database queue. Hopefully I'm doing something hurtfully inhibited here, which is easy to notice for everyone who isn't me!
static void Pacman()
{
Queue<MatchUpdateQueue> waiting = new Queue<MatchUpdateQueue>();
events.WriteEntry("matchqueue worker thread started");
while (!stop)
{
if (waiting.Count == 0)
{
aDataContext db = new aDataContext();
List<MatchUpdateQueue> freshitems = db.MatchUpdateQueues.OrderBy(item => item.id).ToList();
foreach (MatchUpdateQueue item in freshitems)
waiting.Enqueue(item);
db.Dispose();
}
else
{
MatchUpdateQueue item = waiting.Peek();
try
{
int result = ActionHandler.Handle(item);
if (result == -1)
events.WriteEntry("unknown command consumed : " + item.actiontype.ToString(), EventLogEntryType.Error);
waiting.Dequeue();
aDataContext db = new aDataContext();
db.MatchUpdateQueues.DeleteOnSubmit(db.MatchUpdateQueues.Single(i => i == item));
db.SubmitChanges();
db.Dispose();
}
catch (Exception ex)
{
events.WriteEntry("exception while handling item : " + ex.Message, EventLogEntryType.Error);
stop = true;
}
}
if (waiting.Count == 0)
Thread.Sleep(TimeSpan.FromSeconds(10));
}
events.WriteEntry("matchqueue worker thread halted");
}
glipquux
source
share