【问题标题】:Process is fireing before completed when double click双击时进程在完成之前触发
【发布时间】:2021-05-03 21:02:10
【问题描述】:

我遇到了一个函数问题:当我单击一个按钮时,一个代码块正在运行,但是当用户多次单击该按钮或链接时,该代码块运行了多次而没有完成运行。

所以有时它会将数据写入数据库两次。我正在使用 ASP.NET MVC 和 C#。

这是我的示例代码:

[Route("Reservations")]
public ActionResult Index()
{            
    MTAPI mTAPI = new MTAPI();
    mTAPI.VeriAl();

    reservations.EditPassedTourTime();
    reservation.Raccount = reservations.GetAllResAccountLMTD();           

    return View(reservation);
}

public void VeriAl()
{
    int VoucherId = rservations.GetLastVoucher().fldId;

    using (example _db= new example())
    {
        var newdata =_db.Voucher();
        newdata.beforerecordId = VoucherId;
        _db.Voucher.Add(newdata);
        _db.SaveChanges();
    }  
}

【问题讨论】:

  • 您可以在第一次点击后禁用提交按钮(使用 Javascript 和点击处理程序),也可以使用Post/Redirect/Get pattern
  • 我认为这不能解决我的问题,因为代码块可以通过刷新页面来触发,所以如果用户按下 f5 不止一个,我也会遇到同样的问题。感谢您的快速响应。

标签: c#


【解决方案1】:

我找到了解决问题的方法。我愿意接受新的优惠。

我放了一个布尔变量,直到函数被编译为止。

代码;

  [Route("Reservations")]
public ActionResult Index()
{            
    MTAPI mTAPI = new MTAPI();
    mTAPI.VeriAl();
    rservations.EditPassedTourTime();
    reservation.Raccount = rservations.GetAllResAccountLMTD();           
    return View(reservation);
}
  public class dd
    {
       public bool isSearching = false;
    }
 
 public void VeriAl()
{
 bb:
   if (dd.isSearching == true) { System.Threading.Thread.Sleep(1000); goto bb; }
        dd.isSearching = true;
   int VoucherId = rservations.GetLastVoucher().fldId;
   using (example _db= new example())
   {
    var newdata =_db.Voucher();
    newdata.beforerecordId=VoucherId;
    _db.Voucher.Add(newdata);
    _db.SaveChanges();
 dd.isSearching = false;
   }  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    相关资源
    最近更新 更多