开发者

Multiple Thread issue

开发者 https://www.devze.com 2023-02-24 17:25 出处:网络
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.

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号