Click here to Skip to main content
15,887,585 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Where to go to learn wpf? Pin
SledgeHammer015-Jan-15 9:59
SledgeHammer015-Jan-15 9:59 
AnswerRe: Where to go to learn wpf? Pin
Mycroft Holmes5-Jan-15 12:11
professionalMycroft Holmes5-Jan-15 12:11 
GeneralRe: Where to go to learn wpf? Pin
SledgeHammer015-Jan-15 12:58
SledgeHammer015-Jan-15 12:58 
GeneralRe: Where to go to learn wpf? Pin
Mycroft Holmes5-Jan-15 13:21
professionalMycroft Holmes5-Jan-15 13:21 
AnswerRe: Where to go to learn wpf? Pin
Richard MacCutchan5-Jan-15 22:43
mveRichard MacCutchan5-Jan-15 22:43 
AnswerRe: Where to go to learn wpf? Pin
Vimalsoft(Pty) Ltd12-Jan-15 20:56
professionalVimalsoft(Pty) Ltd12-Jan-15 20:56 
AnswerRe: Where to go to learn wpf? Pin
Subramanyam Shankar17-Mar-15 1:04
professionalSubramanyam Shankar17-Mar-15 1:04 
QuestionMultithreaded Windows with Data Binding Pin
Dominick Marciano5-Jan-15 8:30
professionalDominick Marciano5-Jan-15 8:30 
I am working on an application that will need to open multiple windows, each on a separate thread. I have each on a different thread because each window represents a "Ticket" which will need to get data from a server, update a server, as well as other stuff and when all were running on a single thread some of the ticket windows were lagging.

I am able to create the ticket windows and pass arguments, however as soon as I start trying to update properties on the window I get exceptions. Currently each window that needs to be created is created using this method:
VB
Private Shared Sub CreateNewWindow(Of T As Window)(ByVal newThread As Boolean, ByVal onOpened As Action(Of T), ByVal ParamArray args() As Object)
        Dim activateWindow As Action = Sub()
                                           Dim window As T = DirectCast(Activator.CreateInstance(GetType(T), args), T)
                                           AddHandler window.Closed, Sub(s, e)
                                                                         window.Dispatcher.BeginInvokeShutdown(DispatcherPriority.Background)
                                                                     End Sub
                                           window.Show()
                                           If onOpened IsNot Nothing Then
                                               onOpened(window)
                                           End If
                                       End Sub
        If newThread Then
            Dim newWindowThread As New Thread(New ThreadStart(Sub()
                                                                  SynchronizationContext.SetSynchronizationContext(New DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher))
                                                                  activateWindow()
                                                                  System.Windows.Threading.Dispatcher.Run()
                                                              End Sub))
            newWindowThread.SetApartmentState(ApartmentState.STA)
            newWindowThread.IsBackground = True
            newWindowThread.Start()
        Else
            activateWindow()
        End If
    End Sub


And the window's constructor is:
VB
Public Sub New(properties As WindowProperties)
        InitializeComponent()
        _ticket = properties.Ticket
        Me.Top = properties.Top
        SetupWindow()
    End Sub


And the SetupWindow method is:
VB
Private Sub SetupWindow()
       Try
           TitleBarColor = currentSettings.DefaultTicketBrush
           _stop = New ManualResetEvent(False)

           Me.Name = "TicketWindow_" & Ticket.id
           Me.RegisterName(Me.Name, Me)

           If currentSettings.FadeInTicketWindow Then
               Dim fadeInAnimation As New DoubleAnimation
               With fadeInAnimation
                   .From = 0.0
                   .To = 1.0
                   .Duration = New Duration(TimeSpan.FromSeconds(0.75))
                   .AutoReverse = False
               End With

               fadeInStoryboard = New Storyboard
               fadeInStoryboard.Children.Add(fadeInAnimation)
               Storyboard.SetTargetName(fadeInAnimation, Me.Name)
               Storyboard.SetTargetProperty(fadeInAnimation, New PropertyPath(Window.OpacityProperty))
               fadeInStoryboard.Begin(Me)
           End If
       Catch ex As Exception
           Dim m As String = ex.Message
       End Try
   End Sub


Where currentSettings is an global object which is an instance of a AppSettings object which contains some configuration data andTitleBarColor is a property of the window (which implements INotifyPropertyChanged) used for binding:
VB
Public Property TitleBarColor As Brush
        Get
            Return _titleBarColor
        End Get
        Private Set(value As Brush)
            Try
                _titleBarColor = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("TitleBarColor"))
            Catch ex As Exception
                Dim m As String = ex.Message
            End Try
        End Set
    End Property


However with the code this way I get an exception in the CreateWindow method at window.show which says "Must create DependencySource on same Thread as the DependencyObject".

However If I change the TitleBarColor properties Set method to:
VB
LTitleBar.Dispatcher.Invoke(New Action(Sub()
                                           LTitleBar.Fill = value
                                       End Sub))

Then the exception is caught in the property with the message "The calling thread cannot access this object because a different thread owns it."

I would appreciate any assistance in how I can update properties of a window which is on a separate thread but needs to access objects on a different thread (e.g. currentSettings) and still have binding working. I have tried various pieces of code I have found on the web but none of them have worked for me. There was one piece of code that seems to work (no exception thrown) but the bound properties did not update when a change was made to the TitleBarColor property.

Thank you in advance for any assistance with this.
A black hole is where God tried to divide by zero.

There are 10 kinds of people in the world; those who understand binary and those who don't.

AnswerRe: Multithreaded Windows with Data Binding Pin
Afzaal Ahmad Zeeshan9-Jan-15 8:41
professionalAfzaal Ahmad Zeeshan9-Jan-15 8:41 
AnswerRe: Multithreaded Windows with Data Binding Pin
SledgeHammer0111-Jan-15 8:37
SledgeHammer0111-Jan-15 8:37 
QuestionWPF DataGrid with ComBox Bound To Enums - Show Descriptions Pin
Kevin Marois5-Jan-15 7:03
professionalKevin Marois5-Jan-15 7:03 
AnswerRe: WPF DataGrid with ComBox Bound To Enums - Show Descriptions Pin
Pete O'Hanlon5-Jan-15 7:19
mvePete O'Hanlon5-Jan-15 7:19 
QuestionGetting error when i drag&drop in WPF Pin
VisualLive1-Jan-15 17:35
VisualLive1-Jan-15 17:35 
AnswerRe: Getting error when i drag&drop in WPF Pin
Afzaal Ahmad Zeeshan9-Jan-15 8:44
professionalAfzaal Ahmad Zeeshan9-Jan-15 8:44 
AnswerRe: Getting error when i drag&drop in WPF Pin
Vimalsoft(Pty) Ltd11-Jan-15 23:52
professionalVimalsoft(Pty) Ltd11-Jan-15 23:52 
QuestionLoop in Dispatch.Invoke Not Updating UI Pin
Dominick Marciano31-Dec-14 16:15
professionalDominick Marciano31-Dec-14 16:15 
QuestionSelectionChanged Event is not firing on View Load in MVVM Pin
Ashfaque Hussain29-Dec-14 20:15
Ashfaque Hussain29-Dec-14 20:15 
QuestionSilverlight? A future? Pin
Sean McPoland26-Dec-14 23:46
Sean McPoland26-Dec-14 23:46 
AnswerRe: Silverlight? A future? Pin
Richard MacCutchan26-Dec-14 23:57
mveRichard MacCutchan26-Dec-14 23:57 
AnswerRe: Silverlight? A future? Pin
Pete O'Hanlon27-Dec-14 2:03
mvePete O'Hanlon27-Dec-14 2:03 
QuestionWhat are those automation peer in WPF? Pin
Super Lloyd22-Dec-14 0:55
Super Lloyd22-Dec-14 0:55 
AnswerRe: What are those automation peer in WPF? Pin
Pete O'Hanlon22-Dec-14 1:32
mvePete O'Hanlon22-Dec-14 1:32 
AnswerRe: What are those automation peer in WPF? Pin
syed shanu23-Dec-14 17:42
mvasyed shanu23-Dec-14 17:42 
QuestionRouted Event No Responding Pin
Kevin Marois18-Dec-14 9:20
professionalKevin Marois18-Dec-14 9:20 
QuestionBest way to stroke a path with TWO colors? Pin
SledgeHammer0117-Dec-14 11:17
SledgeHammer0117-Dec-14 11:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.