0

I have an ASP.NET Webform application. I added a Ado.net Entity Data Model for my database which has a table named GatewayProviders. My table has 3 columns: Id(int), Description(string), GatewayType(int). I added two methods in webform1.aspx.cs :

 public void update()
    {
        GatewayProvider gp = DB.GatewayProviders.Find(12);
        gp.Description = "pejman";

        DB.SaveChanges();
    }

    public void update2()
    {
        GatewayProvider gp = DB.GatewayProviders.Find(12);
        gp.Description = "nazraz";
        DB.SaveChanges();
    }

I created a button named Button1 to webform1.aspx and i added click-event for button like this :

  protected void Button1_Click(object sender, EventArgs e)
    {
        Thread th = new Thread(new ThreadStart(update));
        Thread th2 = new Thread(new ThreadStart(update2));
        th.Start();
        th2.Start();
    }

assume i publish this application on web, then two client as the same time click on the button, my application give them error!! how can i fix it?

how can i make my code threadsafe?

pmn
  • 2,176
  • 6
  • 29
  • 56
  • Not duplicate - this is specific to the ASP.NET threading model, sorry. – TomTom Jun 17 '14 at 15:00
  • This looks like a concurrency problem, you can solve it by using transactions. Also you shouldn't use threads in web application, it is multithreaded by default. – oleksii Jun 17 '14 at 15:01
  • Actually no. THis totally is based on how ASP.NET works and when it "finishes" a page. See my answer. – TomTom Jun 17 '14 at 15:01
  • @oleksii Could you explain more your point about why we shouldnt use threads in asp.net? Is there any technical limitations? – Oscar Jun 17 '14 at 15:03
  • @Oscar I think OP tries to emulate several users clicking the same button at the same time. In which case this becomes a conflict and is generally solved via transactions. If you have 2 people clicking the same button at the same time, two requests will be enqueued on the server. To handle each request a server uses separate dedicated thread from a thread pool, unless it is under heavy load and is starving for threads. So spawning a new thread in asp.net is ~ to starting a new thread from a thread in a threadpool, which is a bit odd. Make sense? – oleksii Jun 17 '14 at 15:06
  • Which is a scenario that makes zero sense -and in EF 6.1 Savechanges is in a transaction to start with. So it is unclear what is being asked outside the fact that he totally invalidates the asp.net threading model. – TomTom Jun 17 '14 at 15:09
  • Thanks all, but tell me how can i handle this scenario please?? – pmn Jun 18 '14 at 04:07
  • What scenario? How to do something that makes no sense? Get some money from a cash dispenser, burn it. Same result - you did something that makes no sense. The above scenario makes zero sense, come up with one that does. – TomTom Jun 18 '14 at 04:14
  • @TomTom My scenario is : assume i publish this application on web, then two client as the same time click on the button, my application give them error!! how can i fix it? what is your idea? – pmn Jun 18 '14 at 04:24
  • 1
    Then the code does not show the scenario and is totally off and you give zero information for the real question, including the error that you have. Which you seem not to even hav but to imagine you have because you do not understand the basics of threading,asp.net AND sql. – TomTom Jun 18 '14 at 04:45
  • @oleksii I understand your point, but as TomTom said, this test case doesn't describe this scenario, it's a completely different example. BTW, if you have more than one task to run in a button click and those task are independent, I don't see the point why they can't be run in parallel.. – Oscar Jun 18 '14 at 07:14

1 Answers1

0

You know basic ASP.NET? Here is the hint:

ASP.NET is request/Response/done. Click->Sends order to server -> processes -> sends HTML.

in the CLick handler you start threads. Once the handler ends - the HTML is sent, ASP.NET is finished.

In your case:

You start threads, but then the procesing ends before likely they execute. The exact error message likely has interesting info WHICH object is null ;)

You absolutely MUST wait for both threads to finish before ending the click handler, because THEN (once you end the click handler) the HTML is generated and sent to the client.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • Thanks @TomTom, can you give me more explanation or give me some code which how can i handle this scenario? – pmn Jun 18 '14 at 04:05