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

WPF

 
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 
In my application I have a window with a custom look and title bar. The XAML for my window is:
XML
<Window x:Class="TicketWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Ticket Window" Height="300" Width="300"
        WindowStyle="None" ShowInTaskbar="False">

    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="{Binding ActualHeight,ElementName=titlebar}"/>
    </WindowChrome.WindowChrome>
    <DockPanel LastChildFill="True">
        <Border Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}" DockPanel.Dock="Top" Height="25" x:Name="titlebar">
            <TextBlock Text="{Binding Title, RelativeSource={RelativeSource FindAncestor,AncestorType=Window},FallbackValue=Title}" 
                       Margin="10,0,0,0"
                       VerticalAlignment="Center">
                <TextBlock.Effect>
                    <DropShadowEffect Color="White" ShadowDepth="3"/>
                </TextBlock.Effect>
            </TextBlock>
        </Border>
        <Border BorderBrush="LightGray" BorderThickness="1" Padding="4">
            <TextBlock><Run Text="Window content"/><InlineUIContainer>
            		<Button Content="Button" Width="75" Click="Button_Click"/>
            	</InlineUIContainer></TextBlock>
        </Border>
    </DockPanel>
</Window>


I would like to be able to blink the title bar under some circumstances from a second thread using a loop. For testing purposes I am using the button to start the thread to blink the title bar between green and red with a half-second delay in between the changes.

In the button's click event I create a thread and start it:
VB
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
     Try
         Dim thread As New Thread(AddressOf ChangeTitleBarColor)
         thread.Start()
     Catch ex As Exception
         Dim m As String = ex.Message
     End Try
 End Sub


I've only used the Dispatcher a couple of times, so to initially test the ChagneTitleBarColor method and ensure I was using the Dispatch correct I used the following code:
VB
Private Sub ChangeTitleBarColor()
    Try
        titlebar.Dispatcher.Invoke(Threading.DispatcherPriority.Render, Sub()
                                                                            titlebar.Background = Brushes.Green
                                                                        End Sub)
        Thread.Sleep(500)
        titlebar.Dispatcher.Invoke(Threading.DispatcherPriority.Render, Sub()
                                                                            titlebar.Background = Brushes.Red
                                                                        End Sub)
        Thread.Sleep(500)
    Catch ex As Exception
        Dim m As String = ex.Message
    End Try
End Sub


And this worked; the title bar went from the default grey to green, there is a half-second delay, and then the title bar changes to red. Next I tried the following code:
VB
Private Sub ChangeTitleBarColor()
        Try
            titlebar.Dispatcher.Invoke(Threading.DispatcherPriority.Render, Sub()
                                                                                titlebar.Background = Brushes.Green
                                                                                Thread.Sleep(500)
                                                                                titlebar.Background = Brushes.Red
                                                                            End Sub)
        Catch ex As Exception
            Dim m As String = ex.Message
        End Try
    End Sub


However the title bar never turns green with the above code; it goes from grey to red. So I took the the first version of the ChangeTitleBarColor and added a loop so the title bar would switch between green and red indefinitely:
VB
Private Sub ChangeTitleBarColor()
       Try
           While True
               titlebar.Dispatcher.Invoke(Threading.DispatcherPriority.Render, Sub()
                                                                                   titlebar.Background = Brushes.Green
                                                                               End Sub)
               Thread.Sleep(500)
               titlebar.Dispatcher.Invoke(Threading.DispatcherPriority.Render, Sub()
                                                                                   titlebar.Background = Brushes.Red
                                                                               End Sub)
               Thread.Sleep(500)
           End While
       Catch ex As Exception
           Dim m As String = ex.Message
       End Try

And this works fine as well.

I even tried adding another Thread.Sleep(500) after the changing the color to red and a call to an empty action (Private EmptyDelegate As New Action(Sub()End Sub)) using the Dispatch.Invoke before each call to the sleep method of Thread and it still didn't work.

I would appreciate it if someone could explain why if I change the title bar twice in the single invoke statement (as in the second example of the ChangeTitleBarColor below) why the color only changes a single time? Is there a simpler way to switch the title bar between two colors indefinitely from a second thread?

Thank you in advance. Any help would be greatly appreciated.
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.

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 
SuggestionRe: Best way to stroke a path with TWO colors? Pin
Matt T Heffron17-Dec-14 11:39
professionalMatt T Heffron17-Dec-14 11:39 
GeneralRe: Best way to stroke a path with TWO colors? Pin
SledgeHammer0117-Dec-14 12:06
SledgeHammer0117-Dec-14 12:06 
QuestionWPF DataGrid Date Format Pin
Kevin Marois17-Dec-14 7:21
professionalKevin Marois17-Dec-14 7:21 
GeneralRe: WPF DataGrid Date Format Pin
PIEBALDconsult17-Dec-14 8:12
mvePIEBALDconsult17-Dec-14 8:12 
GeneralRe: WPF DataGrid Date Format Pin
Kevin Marois17-Dec-14 8:21
professionalKevin Marois17-Dec-14 8:21 
GeneralRe: WPF DataGrid Date Format Pin
Richard MacCutchan17-Dec-14 9:38
mveRichard MacCutchan17-Dec-14 9:38 
GeneralRe: WPF DataGrid Date Format Pin
Kevin Marois17-Dec-14 9:42
professionalKevin Marois17-Dec-14 9:42 
GeneralRe: WPF DataGrid Date Format Pin
Maciej Los17-Dec-14 11:14
mveMaciej Los17-Dec-14 11:14 

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.