Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Threading WinForm
Hi friends,
 
I am using Asterisk.NET Library to recieve events from an asterisk server..
But, I cant update the forms directly in events as its on another thread. (Cross-threading problem blaablaablaa)
I am now using Invoke methode to update the form controls,,, But it seems very slow (About 5 seconds or more)
How can I make it fast ? Is there any idea other than Invoking to access form controls from another thread. Here is my code...
 
public void New_State_Event(object sender, Asterisk.NET.Manager.Event.NewStateEvent e)
{
        this.Invoke(new MethodInvoker(delegate()
        {
              Label1.Text = e.Channel + " Calling" ;
        }));
}
 
Please Help me.... :(
Posted 16-Oct-13 0:38am
Edited 16-Oct-13 1:53am
v2
Comments
lukeer at 16-Oct-13 7:10am
   
The GUI thread my be busy and therefore not able to respond faster. I had that once caused by a TextBox that was frequently updated.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this, it works fine in my application:
public void New_State_Event(object sender, Asterisk.NET.Manager.Event.NewStateEvent e)
{
     UpdateLabel( e.Channel + " Calling" );
        
}
 
private void UpdateLabel(string textForLabel)
{
  if (this.Label1.InvokeRequired)
  {
     this.Invoke (new Action<string>(UpdateLabel), new object[] {textForLabel});
     return;
  }
  else
     Label1.Text= textForLabel;
}
</string>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

At what rate is "New_State_Event" called from Asterisk.NET ?
 
Every time "New_State_Event" event is called, you invoke a method that is blocking the Main UI Thread and the worker thread where "New_State_Event" is coming from.
 
Use "BeginInvoke" and see if that helps.
  Permalink  
v2

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

  Print Answers RSS
0 Mika Wendelius 191
1 Peter Leow 150
2 TheRealSteveJudge 129
3 Abhinav S 125
4 Suvendu Shekhar Giri 100
0 Sergey Alexandrovich Kryukov 8,513
1 OriginalGriff 6,526
2 Peter Leow 3,657
3 Zoltán Zörgő 3,426
4 Richard MacCutchan 2,417


Advertise | Privacy | Mobile
Web03 | 2.8.150123.1 | Last Updated 16 Oct 2013
Copyright © CodeProject, 1999-2015
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