Results 1 to 4 of 4

Thread: [RESOLVED] WPF DispatcherTimer Threading issue

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Resolved [RESOLVED] WPF DispatcherTimer Threading issue

    Ok - I've got this code

    Code:
    private void SendToReader(object sender, EventArgs ea) //(object objectState)
    {
        //long tc = System.Threading.Interlocked.Increment(ref TimerClick);
        lock (mainTimer)
        {
            //mainTimer.Change(System.Threading.Timeout.Infinite, 250);
            mainTimer.IsEnabled = false;
        }
    .
    .
    .
    bool doContactReader = true;
    while (doContactReader)
    {
        if (cl2.asyncPostText("sessionid", ref rdFSOb, prefixes, cqMS, ref returnMessage))
        {
            doContactReader = false;
            SessionId = Convert.ToInt64(rdFSOb.FNTagsCollection[0]);
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (System.Threading.ThreadStart)delegate()
            {
                txtSearch.Text = "";
                txtSearch.IsEnabled = true;
                txtSearch.Focus();
            });
        }
        else
        {
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (System.Threading.ThreadStart)delegate()
            {
                if (MessageBox.Show("Reader is not responding - try again?", "Try Again or Cancel", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.Cancel)
                {
                    doContactReader = false;
                    txtSearch.Text = "";
                }
            });
        }
    }
    Running in a DispatcherTime

    Code:
                mainTimer = new DispatcherTimer(DispatcherPriority.Normal);
                mainTimer.Interval = new TimeSpan(250);
                mainTimer.Tick += new EventHandler(SendToReader);
                mainTimer.IsEnabled = true;
    And if you look at the image I've attached - if the reader responds it nicely clears the .Text and Enables it and sets the Focus.

    So Break #1 hits

    If the reader does not respond - as I am showing in the image that is attached - the BREAK is actually at the circled spot. That's good.

    But it never gets to Break #2 - never pops up the message box.

    What am I doing wrong?
    Attached Images Attached Images  

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: WPF DispatcherTimer Threading issue

    I should add to this that if I do START THE READER while it's waiting - I will get BREAK #2 to hit - as many times as the 250 ms timer ran - yuk!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: WPF DispatcherTimer Threading issue

    Actually - this code is blocking - I'm getting an HOUR GLASS.

    If I COMMENT OUT the contents of the TIMER function - it does not block.

    Why would what I'm doing in the TIMER make the UI block?

    Can anyone help me understand the WPF dispatcher model and using TIMER's in WPF like this?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: WPF DispatcherTimer Threading issue

    omg - I figured this out!

    The MESSAGEBOX does not get put into the dispatcher for an invoke!!

    Each object in WPF inherits from the dispatcher - I just can use the MESSAGEBOX and it works perfectly!

    Wow - I love WPF!

    Code:
    bool doContactReader = true;
    while (doContactReader)
    {
        if (cl2.asyncPostText("sessionid", ref rdFSOb, prefixes, cqMS, ref returnMessage))
        {
            doContactReader = false;
            SessionId = Convert.ToInt64(rdFSOb.FNTagsCollection[0]);
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (System.Threading.ThreadStart)delegate()
            {
                txtSearch.Text = "";
                txtSearch.IsEnabled = true;
                txtSearch.Focus();
            });
        }
        else
        {
            if (MessageBox.Show("Reader is not responding - try again?", "Try Again or Cancel", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.Cancel)
            {
                doContactReader = false;
                txtSearch.Text = "";
            }
            //this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (System.Threading.ThreadStart)delegate()
            //{
            //});
        }
    }
    Marking this resolved!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width