简体   繁体   English

在 asp.net 中运行线程时启用和禁用按钮

[英]Enable and disable button while running thread in asp.net

In the button click in web application asp.net, i have created a thread.Inside the thread, I am trying to disable the button and do some background jobs.在按钮中单击 web 应用程序 asp.net 中的按钮,我创建了一个线程。在线程内部,我试图禁用按钮并执行一些后台工作。 Background jobs working perfectly but button disabling is not working.后台作业运行良好,但按钮禁用不起作用。 MY CODE IS:我的代码是:

private void generteBtn_Click(object sender, EventArgs e)
{
   new Thread (() =>
    {
       btn.enabled = false;
       // thread work start here;
       //emailing
    }.start();
     
}

My question is how can I disable buttons while the threads are working and re enable generteBtn_Click after the threads finished.我的问题是如何在线程工作时禁用按钮并在线程完成后重新启用generteBtn_Click。

because the thread that you created here is not the UI thread and to access any element in the UI thread you can use:因为您在此处创建的线程不是 UI 线程,并且要访问 UI 线程中的任何元素,您可以使用:

Application.Current.Dispatcher.Invoke(() =>
{
    btn.enabled = false;
});

and for more details you can check these links:有关更多详细信息,您可以查看以下链接:

1- How do I get the UI thread's Dispatcher? 1- 如何获取 UI 线程的调度程序?

2- Dispatcher.Dispatch on the UI thread 2- UI 线程上的 Dispatcher.Dispatch

Hide/show the buttion BEFORE you start the next process/thread.在开始下一个进程/线程之前隐藏/显示按钮。

eg:例如:

       btn.enabled = false;
      new Thread (() =>
      {
      // thread work start here;
      //emailing
      }.start();

That way, the button is disabled, process starts, as always then a whole new fresh copy of the web page is then sent back to the client side.这样,按钮将被禁用,进程将一如既往地启动,然后将 web 页面的全新副本发送回客户端。

If you need to update that page?如果您需要更新该页面? Then you have really only 2 practial choices.那么你真的只有两个实际的选择。

You can start a timer on the page - call a ajax routine, and it will have to check say some session() value that the process sets = "done".您可以在页面上启动一个计时器 - 调用 ajax 例程,它必须检查进程设置的某个 session() 值 =“done”。

The other way would be to introduce signalR into your applcation.另一种方法是将 signalR 引入您的应用程序。

https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/introduction-to-signalr#:~:text=What%20is%20SignalR%3F%20ASP.NET%20SignalR%20is%20a%20library,process%20of%20adding%20real-time%20web%20functionality%20to%20applications . https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/introduction-to-signalr#:~:text=What%20is%20SignalR%3F%20ASP.NET%20SignalR%20is %20a%20library,process%20of%20adding%20real-time%20web%20functionality%20to%20applications

I have a number of processing routines.我有许多处理例程。 I start the process, and the I start a timeer on the page.我开始这个过程,然后我在页面上启动一个计时器。 It re-freshens a update panel every 1 second until such time the session("MyProcessDone") = true, and then I stop the timer at that point (and of course update the spinner/animated gif).它每 1 秒重新刷新一次更新面板,直到此时 session("MyProcessDone") = true,然后我在该点停止计时器(当然更新微调器/动画 gif)。 So, you can start a timer on the web page - and check some session() value, or as noted, introduce signalR into your applicaton.因此,您可以在 web 页面上启动一个计时器 - 并检查一些 session() 值,或者如前所述,将 signalR 引入您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM