.NET Backgroundworker对象的线程优先级 [英] .NET Backgroundworker Object's Thread Priority

查看:433
本文介绍了.NET Backgroundworker对象的线程优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在正在开发的应用程序中使用.NET Backgroundworker对象.

I am trying to use the .NET Backgroundworker Object in an application I am developing.

Internet上的所有资料都说该对象在后台"运行,但是我无法确认该后台线程确实在低优先级"模式下运行.之所以会出现此问题,是因为在Windows(我假设)中,后台任务可以以正常"或低于正常"或低"优先级模式运行.

All the material on the internet say that this object runs in the "background" but nowhere have I been able to confirm that this background thread indeed runs in a "low priority" mode. This question arises because in Windows (I assume) a background task can run in a 'normal' or 'below normal' or 'low' priority mode.

在我的应用程序中,我尝试通过在DoWork函数中自己设置优先级,方法是调用 ...

In my application, I tried to set the priority myself inside the DoWork function by calling ...

Thread.CurrentThread.Priority=ThreadPriority.Lowest

...

但这似乎没有效果.后台工作人员会忽略此呼叫吗?

but this seems to have no effect. Does the backgroundworker ignore this call?

我想解释更多:

我的应用程序是一个Internet客户端,它从腔室收集有关温度,湿度等的实时数据,并使用

My application is an internet client that collects real-time data on temperature,humidity etc from a chamber and uploads to a web page (not a web service) using

system.net.webclient.UploadValuesAsync(...)通话

我已经编写了应用程序,以便客户端GUI从会议厅收集数据,给数据加上时间戳,然后像这样将其排队等待上载

I have written the application such that the client GUI collects the data from the chamber, time-stamps them and then queues them for upload like so

...

Synclock objlock
    debug.print("Queueing reading...")
    dataQ.Enque(reading)
End Synclock
...

backgroundworker的Dowork函数出队,然后像这样上载...

The backgroundworker's Dowork function dequeues and then uploads like so...

..............

..............

Do
        if dataQ.count() <> 0 then
            Synclock objlock
              reading = dataQ.DeQue()
            End Synclock
            Dim client As New System.Net.WebClient
            ...put the reading in NameValueCollection and upload to server page
            req = new NameValueCollection
            ...
            ...
            client.UploadValuesAsync(uri, "POST", req)
        endif
        thread.sleep(1) 'without this line the CPU usage goes upto 100% and seems to slow other tasks!
    Loop

.......

当我运行程序时,我发现只要调用UploadValuesAsync,调试窗口就会停止打印.我还添加了调试语句,以随时查看队列中有多少读数.如果此任务确实以低优先级运行,我希望看到队列数随着数据的获取而迅速增加,然后仅在前台空闲且未获取数据时才减少.但这种情况并非如此.一旦将读数添加到队列中,便将其出队并上载.因此,队列计数始终为1或0!

When I run the program I find that whenever the UploadValuesAsync is called the print out the debug window stops. I had also added debug statements to see how many readings are in the queue at any time. It this task is truly run in a low priority, I expected to see the queue count increase rapidly as data is acquired and then decrease only when foreground is idle and data is not being acquired. But this is not the case. As soon as a reading is added to the queue it is dequeued and uploaded. So the queue count is always is either 1 or 0!

我的方法有问题吗?我应该完全不使用背景工人对象吗?

Is there something wrong in my approach? Should I not be using the background-worker object at all?

顺便说一句,这是在运行Windows XP的双核笔记本电脑中.

BTW, this is in a dual-core laptop running Windows XP.

推荐答案

只需添加Jon和Marc已经说过的话:

Just to add to what Jon and Marc have already said:

背景线程的优先级较低.前台线程和后台线程之间的区别在于,一旦不再运行前台线程,CLR将关闭进程.线程池线程是后台线程.

Background threads do not have lower priority. The difference between foreground and background threads is that the CLR will shutdown the process once no more foreground threads are running. Thread pool threads are background threads.

您实际上可以设置线程池线程的优先级,但是由于几乎无法控制哪个线程池线程将实际运行任务,因此不建议这样做.如果需要特定优先级的线程,则应使用线程"类型创建它们,并根据需要在实例上设置优先级.

You can actually set the priority of a thread pool thread, but as you have next to no control of which thread pool thread will actually run your task it is not advisable to do so. If you need threads of a specific priority you should create them using the Thread type and set the priority on the instance as desired.

这篇关于.NET Backgroundworker对象的线程优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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