Aug 15th, 2013, 06:34 AM
#1
[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
Aug 15th, 2013, 06:59 AM
#2
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!
Aug 15th, 2013, 07:06 AM
#3
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?
Aug 15th, 2013, 09:33 AM
#4
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!
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width