Search Results

Search found 3 results on 1 pages for 'digitaldias'.

Page 1/1 | 1 

  • Using INotifyPropertyChanged in background threads

    - by digitaldias
    Following up on a previous blog post where I exemplify databinding to objects, a reader was having some trouble with getting the UI to update. Here’s the rough UI: The idea is, when pressing Start, a background worker process starts ticking at the specified interval, then proceeds to increment the databound Elapsed value. The problem is that event propagation is limeted to current thread, meaning, you fire an event in one thread, then other threads of the same application will not catch it. The Code behind So, somewhere in my ViewModel, I have a corresponding bethod Start that initiates a background worker, for example: public void Start( ) { BackgroundWorker backgroundWorker = new BackgroundWorker( ); backgroundWorker.DoWork += IncrementTimerValue; backgroundWorker.RunWorkerAsync( ); } protected void IncrementTimerValue( object sender, DoWorkEventArgs e ) { do { if( this.ElapsedMs == 100 ) this.ElapsedMs = 0; else this.ElapsedMs++; }while( true ); } Assuming that there is a property: public int ElapsedMs { get { return _elapsedMs; } set { if( _elapsedMs == value ) return; _elapsedMs = value; NotifyThatPropertyChanged( "ElapsedMs" ); } } The above code will not work. If you step into this code in debug, you will find that INotifyPropertyChanged is called, but it does so in a different thread, and thus the UI never catches it, and does not update. One solution Knowing that the background thread updates the ElapsedMs member gives me a chance to activate BackgroundWorker class’ progress reporting mechanism to simply alert the main thread that something has happened, and that it is probably a good idea to refresh the ElapsedMs binding. public void Start( ) { BackgroundWorker backgroundWorker = new BackgroundWorker( ); backgroundWorker.DoWork += IncrementTimerValue; // Listen for progress report events backgroundWorker.WorkerReportsProgress = true; // Tell the UI that ElapsedMs needs to update backgroundWorker.RunWorkerCompleted += ( sender, e ) => { NotifyThatPropertyChanged( "ElapsedMs" ) }; backgroundWorker.RunWorkerAsync( ); } protected void IncrementTimerValue( object sender, DoWorkEventArgs e ) { do { if( this.ElapsedMs == 100 ) this.ElapsedMs = 0; else this.ElapsedMs++; // report any progress ( sender as BackgroundWorker ).ReportProgress( 0 ); }while( true ); } What happens above now is that I’ve used the BackgroundWorker cross thread mechanism to alert me of when it is ok for the UI to update it’s ElapsedMs field. Because the property itself is being updated in a different thread, I’m removing the NotifyThatPropertyChanged call from it’s Set method, and moving that responsability to the anonymous method that I created in the Start method. This is one way of solving the issue of having a background thread update your UI. I would be happy to hear of other cross-threading mechanisms for working in a MCP/MVC/MVVM pattern.

    Read the article

  • New Geek!

    - by digitaldias
    Hi everyone! New geek on the block, treat me gently :)  My main focus will be developing WPF, Silverlight and SharePoint (2010) solutions with TDD and agile methods.

    Read the article

  • Mac OS needs Windows Live Writer – badly!

    - by digitaldias
    I recently bought a new  Macbook Pro (the 13” one) to dive into a new world of programming challenges as well as to get a more powerful netbook than my Packard Bell Dot which I’ve been using since last year. I’ve had immense pleasure using the netbook format and their small size in meetings (taking notes with XMind), surfing “anywhere”, and, of course blogging with windows live writer. So far the Mac is holding up, it’s sleek, responsive, and I’ve even begun looking at coding in Objective C with it, but in one arena, it is severely lacking: Blogging software. There is nothing that even comes close to Live Writer for getting your blog posts out. The few blogger applications that do exist on mac both look and feel medieval in comparison, AND some even cost money! It looks like some mac users actually install a virtual machine on their mac to run Windows XP just so they can use WLW. I’m not that extreme; instead, I’m hoping that the WLW team will write it’s awesome application as a Silverlight 4 app. That way, it would run on Mac and Windows (as a desktop app). I wonder if it will ever happen though…   PS: The image is of me, took it with the built-in camera on the mac and emailed it to the windows PC that I am writing on :)

    Read the article

1