C # Custom thread modification UI (i)

Source: Internet
Author: User

In WPF, the interface displays the modification of the data through the UI thread, and if an attempt is made to modify the value of the control directly from another thread to throw an exception, "The calling thread cannot access this object because another thread owns the object."

Example: http://www.cnblogs.com/tianma3798/p/5762016.html

Solution 1: Send the action to the dispatcher object via invoke or BeginInvoke, delegate dispatcher to perform UI actions, each UI control has dispatcher object

code example:

XAML Code

<ProgressBarx:name= "Progressone"Minimum= "0"Maximum= "1"Height= "+"Margin= "10,85,10,0"VerticalAlignment= "Top"/><!--binds the value of the progress bar and formats it as a percentage -<Labelx:name= "Label"Content="{Binding Elementname=progressone,path=value}"Contentstringformat="{}{0:P2}"HorizontalAlignment= "Left"Margin= "110,120,0,0"FontWeight= "Bold"Foreground= "Red"VerticalAlignment= "Top"/>

C # code

//start a career modify UI dataTask.run (() ={    inti =0;  while(true) {i++; if(i = = -) I=0; //define modify UI Delegateaction<int> action1 = (q) = ={Progressone.value= q/100.0;        };        ProgressOne.Dispatcher.BeginInvoke (Action1, i); Thread.Sleep ( -); }});

Show:

Solution 2: Use WPF's two-way binding to modify the source data in a custom thread to automatically notify the UI thread to modify the interface display

This way has a benefit, only concerned about the data modification, the interface is more than the interface using the data automatically modified

More two-way binding: http://www.cnblogs.com/tianma3798/p/5765464.html

code example:

XAML Code

<Grid>    <Sliderx:name= "Slider"Height= "+"Value="{Binding Path=result,mode=twoway}"Minimum= "0"Maximum= "+"Margin= "35,120,25,0"VerticalAlignment= "Top"/>    <Labelx:name= "Label"Content="{Binding Path=result,mode=twoway}"Contentstringformat= "Current value: {0}"HorizontalAlignment= "Left"FontWeight= "Bold"Foreground= "Red"Margin= "95,165,0,0"VerticalAlignment= "Top"/></Grid>

C # code

//Two-way binding dataNumberdata data =Newnumberdata (); slider. DataContext=Data;label. DataContext=data;//initiates thread modifies bound data, interface content is modified synchronouslyTask.run (() ={    inti =0;  while(true) {i++; if(i = = -) i =0; //modifying bidirectional binding PropertiesData. Result =i; Thread.Sleep ( -); }});
/// <summary>///implement INotifyPropertyChanged interface, notify/// </summary> Public classnumberdata:inotifypropertychanged{ Public EventPropertyChangedEventHandler propertychanged; Private int_result;  Public intResult {Get{return_result;} Set{_result=value; if( This. PropertyChanged! =NULL) propertychanged ( This,NewPropertyChangedEventArgs ("Result")); }    }}

More Asynchronous Modification ui:http://www.cnblogs.com/tianma3798/p/5766691.html

C # Custom thread modification UI (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.