Click here to Skip to main content
12,949,826 members (89,736 online)
Rate this:
 
Please Sign up or sign in to vote.
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 11:45am
Updated 14-Sep-12 11:51am
v2
Comments
Wes Aday 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
yayaNasr 14-Sep-12 18:24pm
   
how can you convert my code using Dispatcher sorry i am new in using dispatcher and threading
yayaNasr 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

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 14-Sep-12 18:52pm
   
5
Sergey Alexandrovich Kryukov 14-Sep-12 19:32pm
   
Thank you, Wes.
--SA
yayaNasr 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
yayaNasr 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 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
Top Experts
Last 24hrsThis month
OriginalGriff 5,749
CHill60 3,380
Maciej Los 2,953
Jochen Arndt 1,935
ppolymorphe 1,820


Advertise | Privacy | Mobile
Web02 | 2.8.170524.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2017
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