Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :
C#
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
Updated 14-Sep-12 11:51am
v2
Comments
[no name] 14-Sep-12 17:52pm    
Use the Dispatcher
Sergey Alexandrovich Kryukov 14-Sep-12 18:23pm    
Why did you use InvokeRequired even in the original code? In most cases, there is no need.
--SA
Yasser El Shazly 14-Sep-12 18:24pm    
how can you convert my code using Dispatcher sorry i am new in using dispatcher and threading
Yasser El Shazly 14-Sep-12 18:25pm    
so what should i use Sergey ?
Sergey Alexandrovich Kryukov 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

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
 
Share this answer
 
Comments
[no name] 14-Sep-12 18:52pm    
5
Sergey Alexandrovich Kryukov 14-Sep-12 19:32pm    
Thank you, Wes.
--SA
Yasser El Shazly 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<string>(buffer_TimedOut), new Object[] { value });
}
Sergey Alexandrovich Kryukov 14-Sep-12 22:21pm    
Very good. Any problems?
--SA
Yasser El Shazly 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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900