Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4323872
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:03:29+00:00 2026-05-21T09:03:29+00:00

Here I am using multi threading and linq to sql. Here I upload my

  • 0

Here I am using multi threading and linq to sql.

Here I upload my code snippet:

public class PostService
{ 
    MessageRepository objFbPostRespository = new MessageRepository();
    public void callthreads()
    {
        for (int i = 0; i < 100; i++)
        {
            Thread th = new Thread(postingProcess);
            th.Start();
        }
    }

    public void postingProcess()
    {
        objFbPostRespository.AddLog("Test Multithread", DateTime.Now);
    }
}

Message Repository class

class MessageRepository
{        
    DataClassesDataContext db_Context = new DataClassesDataContext();
    public void AddLog(string Message, DateTime CurrentDateTime)
    {
        FbMessgaeLog FbMessage = new FbMessgaeLog
        {
            Message = Message,
            Time = CurrentDateTime                
        };
        db_Context.FbMessgaeLogs.InsertOnSubmit(FbMessage);
        db_Context.SubmitChanges();
    }
}

When I run it without threads then it’s work fine after include thread I was got following error msg:

Error: An item with the same key has already been added.

Thanks in advance…:)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:03:30+00:00Added an answer on May 21, 2026 at 9:03 am

    You cannot use a LINQ DataContext in concurrent fashion:

    Any instance members are not
    guaranteed to be thread safe.

    Therefore you need to either serialize access (lock) which will be horribly inefficient, or better use a separate context in each thread:

    public class PostService
    { 
        public void callthreads()
        {
            for (int i = 0; i < 100; i++)
            {
                Thread th = new Thread(postingProcess);
                th.Start();
            }
        }
    
        public void postingProcess()
        {
            using (MessageRepository objFbPostRespository = new MessageRepository())
            {
               objFbPostRespository.AddLog("Test Multithread", DateTime.Now);
            }
        }
    }
    

    I also hope, for your own sake, that your test has actual logic to wait for the test threads to complete before shutting down… And, of course, properly implement IDisposable in your repository and dispose the context so that the DB connection get placed back in the pool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the jQuery multi-file upload plugin found here: http://www.fyneworks.com/jquery/multiple-file-upload/ I don't see in
I am very inexperienced using multi-threading techniques, but here is what I have tried:
New developer here,Im using the Custom Image Picker by ray wenderlich. But what I
Here I am again with questions about multi-threading and an exercise of my Concurrent
I was trying to implement small time delays in multithreading code using boost::this_thread::sleep. Here
When using a ReadWriteLockSlim for multi-threading support, do I need to EnterWriteLock when using
Here's the simplest multi threading example I found so far: import multiprocessing import subprocess
i have a problem when using my code with threading, the problem is apparently
I'm trying to figure out multi-threading programming in python. Here's the simple task with
I'm new to multi-threading and I have been doing a Proof-of-Concept, I've also 'discovered'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.