Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0 VS2010
hi guys , in Windows application and i can use if(AnyControl.invokeRequired) .. do some thing
but here in WPF can not found this property
any one help my to convert this Code into WPF Code
the code is :
void buffer_TimedOut(String value)
{
    if (this.InvokeRequired)
    {
        this.BeginInvoke(new Action<String>(buffer_TimedOut), new Object[] { value });
    }
    else
    {
        if (value.Length == 13)
        {
    // Barcode!
        }
        else
        {
            // Typed text...append to Textbox f.e.
        }
    }
 
any one help me in doing that ... thanks
Posted 14-Sep-12 12:45pm
Edited 14-Sep-12 12:51pm
v2
Comments
Wes Aday at 14-Sep-12 17:52pm
   
Use the Dispatcher
Sergey Alexandrovich Kryukov at 14-Sep-12 18:23pm
   
Why did you use InvokeRequired even in the original code? In most cases, there is no need.
--SA
yayaNasr at 14-Sep-12 18:24pm
   
how can you convert my code using Dispatcher sorry i am new in using dispatcher and threading
yayaNasr at 14-Sep-12 18:25pm
   
so what should i use Sergey ?
Sergey Alexandrovich Kryukov at 14-Sep-12 19:34pm
   
Didn't Wes and I answered? Please see my answer and ask if you have any further questions; or accept it formally (green button), OK?
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

For most applications, you don't need it even in System.Windows.Forms.Control, because this property will always return true if called from any thread other then the UI thread using a control, and in most designs you know that; this property is only used in some method which can be called from either UI or non-UI thread, which is relatively rare.
 
With WPF, you need to use the class Dispatcher:
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx[^].
 
Please see this article which give sufficient introduction:
http://msdn.microsoft.com/en-us/magazine/cc163328.aspx[^].
 
—SA
  Permalink  
Comments
Wes Aday at 14-Sep-12 18:52pm
   
5
Sergey Alexandrovich Kryukov at 14-Sep-12 19:32pm
   
Thank you, Wes.
--SA
yayaNasr at 14-Sep-12 21:05pm
   
so it will converted like that
if (this.Dispatcher.CheckAccess())
{
try
{
if (value.Length == 13)
{
System.Windows.Forms.MessageBox.Show(Code.ToString());
Code = null;
}
else
{
string temp = this.buffer.Get().ToString();
if (temp.Contains('D'))
{
this.Code += temp[1];
}
else
{
this.Code += temp;
}
}
}
catch
{
// do nothing
}
}
else
{
this.Dispatcher.BeginInvoke(new Action(buffer_TimedOut), new Object[] { value });
}
Sergey Alexandrovich Kryukov at 14-Sep-12 22:21pm
   
Very good. Any problems?
--SA
yayaNasr at 15-Sep-12 9:00am
   
it does not work for me , hey the problem is i am trying to differentiate between barcode key press and keyboard press some one told me i can do that depending up on the speed of reading the code
that is the link of the Example http://codereview.stackexchange.com/questions/15570/differentiate-a-keyboard-scanner-from-keyboard-timeoutbuffer
but it does not differentiate between Keyboard and barcode have you a solution for my problem
Sergey Alexandrovich Kryukov at 16-Sep-12 23:43pm
   
Didn't you misplace this comment? I know the barcode software problem. Did you ask a question on it, too? Your question on thread dispatching was answered, and you are talking about totally different thing. Or are they related?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100