将工作线程作为后台进程时出现问题,当线程在进程中时,页面必须同时允许其他操作. [英] Problem with working thread as background process, page must allow other operation simultaneously when thread is in-process.

查看:72
本文介绍了将工作线程作为后台进程时出现问题,当线程在进程中时,页面必须同时允许其他操作.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我通知您,我是线程概念的新手.
请注意,我正在使用Visual Studio 2010(框架:4.0).


我要实现的是,创建一个具有三个按钮和一个标签的网页.和两个网格来加载数据.

按钮..
1. 启动线程-单击此按钮时,线程应该启动.
2. 将数据加载到网格1中(无论您要执行什么操作,但都必须在服务器端进行操作)
3. 将数据加载到网格2

标签将显示线程状态,即线程结束时线程已启动.


当线程操作正在进行时,并且如果用户单击在网格1中加载数据",则应该加载数据. 如果用户单击在网格2中加载数据",则应加载数据.

:confused:问题是我面临的问题是,直到正在进行线程操作为止.
我无法在Grid中加载数据.

以下代码我已用于启动线程

First of all let me inform you, I am new to Threading concept.
Please note that I''m using Visual Studio 2010 (Framework : 4.0).


What I have to achieve is, create web page having three buttons and one label. and two grid to load data.

Buttons..
1. Start Thread - On Click of this button thread should start.
2. Load data in Grid 1 (Whatever operation you want but it must be server side)
3. Load data in Grid 2

Label that will display status of thread i.e. Thread started, when thread is over thread completed.


When Thread operation is going on, and If User click "Load data in Grid 1", it should load data..
If user click "Load data in grid 2", data should be loaded.

:confused:Problem what I am facing is that, until thread operation is in process.
I cant Load data in Grid.

Following code i have used for Start thread

protected void btnStartThread_Click(object sender, EventArgs e)
    {
        BackgroundWorker workerLocal = new System.ComponentModel.BackgroundWorker();
        workerLocal.DoWork += new DoWorkEventHandler(DoWork);
        workerLocal.WorkerReportsProgress = false;
        workerLocal.WorkerSupportsCancellation = true;
        workerLocal.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(WorkerCompleted);

        // Calling the DoWork Method Asynchronously
        workerLocal.RunWorkerAsync();
    }


private static void DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            for (index = 0; index < 50000; index++)
            {
                // Here i am inserting record in database.
                // Just value of Index for testing purpose
            }
        }
        catch (Exception)
        {
            throw;
        }
    }


private static void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
      //Here i cant access LABEL to update status of thread
  }




谢谢..




Thanks in Advance..

推荐答案

我认为,应该在类的构造函数中实例化BackgroundWorker,而不是在类的构造函数中实例化.按钮.所有事件处理程序也可以在构造函数本身中声明.

完成上述操作后,调用workerLocal.RunWorkerAsync().它还带有参数,因此您可以使用它们将其传递给DoWork函数,该函数可以使用e.Argument接收它.然后,可以将DoWork的结果传递给WorkerCompleted函数.因此,在DoWork中提供e.Result =您的结果",您可以在带有e.Result的WorkerCompleted中接收结果.
现在,它应该访问标签.

很少链接:

http://www.dotnetfunda.com/articles/article613 -background-processes-in-asp-net-web-applications-.aspx [ http://www.c-sharpcorner.com/UploadFile/LivMic/BGWorker07032007000515AM/BGWorker.aspx [ ^ ]

http://www.codeproject.com/KB/web-cache/NETBackgroundWorker.aspx [ ^ ]
In my opinion, the BackgroundWorker should be instantiated in the constructor of your class rather than doing it in a Button. All the event handlers can also be declared there in the constructor itself.

After doing the above, call the workerLocal.RunWorkerAsync(). It also takes arguments, so you can use them to pass it to the DoWork function which can recieve it using e.Argument . Then the result of the DoWork can be passed to the WorkerCompleted function .So give e.Result="Your result" in DoWork and you can recieve the result in the WorkerCompleted with e.Result.
Now it should access the Label.

Edit : Few links :

http://www.dotnetfunda.com/articles/article613-background-processes-in-asp-net-web-applications-.aspx[^]

http://www.c-sharpcorner.com/UploadFile/LivMic/BGWorker07032007000515AM/BGWorker.aspx[^]

http://www.codeproject.com/KB/web-cache/NETBackgroundWorker.aspx[^]


我面临的问题是,直到正在进行线程操作为止.
我无法在Grid中加载数据.


当您单击按钮以加载数据时,到底发生了什么?一直挂到线完成吗?还是什么都不做?

-检查您是否没有通过DoWork方法访问任何UI组件.
-如果要同时从DoWork和UI线程访问数据库,则可以使用insice DoWork中的System.Threading.Thread.Sleep方法使主UI线程具有更高的响应速度.
除非您提供有关在线程和按钮处理程序中执行的操作的更多详细信息,否则我将无济于事.
Problem what I am facing is that, until thread operation is in process.
I cant Load data in Grid.


What''s happening exactly when you click the button to load data? Is is hanging till the thread finishes? Or doesn''t it do anything?

- Check that you are not accessing any UI components from the DoWork method.
- If you are accessing data base from both DoWork and UI thread, you can make the main UI thread more responsive using System.Threading.Thread.Sleep method from insice DoWork.

Unless you give more details about what you do in the thread and the button handler, I can''t help further.


这篇关于将工作线程作为后台进程时出现问题,当线程在进程中时,页面必须同时允许其他操作.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆