多线程问题

发布于 2024-10-31 21:20:37 字数 243 浏览 1 评论 0原文

场景: -

您有一个从不同服务器获取源(数据)的应用程序。您的应用程序正在为每个服务器维护多个线程(以获取提要)以及一个用于 GUI 的线程。 您的 GUI 有一个在不同线程中运行的三个进度条(每个服务器一个)。

问题: -

如果 CPU 足够繁忙以获取提要,那么每次从任何服务器接收提要时,如何使进度条刷新。

约束: -

您不能更改架构或接口。 GUI 线程不可用于服务器线程。

Scenario : -

You have an application which is getting feeds(data) from different servers. Your application is maintaining multiple threads for each server(to get feeds) plus one thread for GUI.
Your GUI is having a three progress bar (one for each server) running in a different thread.

Question: -

If the CPU is busy enough to get the feed, how would you make your progress bar to refresh each time you receive the feed from any server.

Constraint: -

You are not allowed to change the architecture or interfaces.
GUI Thread is not available to Server Threads.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别再吹冷风 2024-11-07 21:20:37

严格遵守这些约束,您可以在 ProgressBar.BindingContext 上使用 DataBinder 并将其绑定到具有进度值的自定义类,此自定义类实现 INotifyPropertyChanging 接口。然后,您设置三个静态变量,一个用于您设置的每个数据绑定(每个进度条一个)。然后只增加静态变量的值。

请参阅:http://msdn.microsoft.com/en-us /library/system.componentmodel.inotifypropertychanging.aspx

其他选项是使用委托,它不需要引用生成控件的线程,一般来说,它们似乎是处理多线程的首选方法,并且ui 更新(vb.net 中的示例只是因为):

protected delegate sub updateProgressdelegate(ByVal control As ProgressBar, byVal value as integer)

protected shared sub updateProgress(ByVal control As ProgressBar, byVal value as integer)
    if(control.invokeRequired) then
        control.invoke(new updateProgressDelegate(addressof updateprogress), control, value)
        exit sub
    end if
    control.value = value

    'these aren't really needed but some people like to do them...
    control.invalidate()
    control.refresh()
end sub

有关委托信息,请参阅此:
http://msdn.microsoft.com/en-us/magazine/cc301810.aspx

另外,如果是家庭作业,则只需用家庭作业来标记它,如果是“考试学习”,则尝试这样标记,诚实可以让您获得更多答案。如果不是家庭作业问题,则提供代码示例,并且可能不要像带回家考试一样格式化问题。

Strictly by those constraints, you can use a DataBinder on the ProgressBar.BindingContext and bind it to a custom class that has a value for progress, this custom class implements the INotifyPropertyChanging interface. You then setup three static variables, one for each data binding you setup (one for each progress bar). then just incriment the value of the static variable.

See: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanging.aspx

Other option is to use delegates, which do not require a reference to the thread which spawned the control, in general they seem to be the preferred way to handle multi-threading and ui updates (example in vb.net just because):

protected delegate sub updateProgressdelegate(ByVal control As ProgressBar, byVal value as integer)

protected shared sub updateProgress(ByVal control As ProgressBar, byVal value as integer)
    if(control.invokeRequired) then
        control.invoke(new updateProgressDelegate(addressof updateprogress), control, value)
        exit sub
    end if
    control.value = value

    'these aren't really needed but some people like to do them...
    control.invalidate()
    control.refresh()
end sub

see this for delegate info:
http://msdn.microsoft.com/en-us/magazine/cc301810.aspx

Also, if homework then just tag it with homework, if 'exam study' then try to tag as such, being honest gets you more answers. If not a homework question then provide code samples and maybe don't format the question like it's from a take-home exam.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文