0

I'm developing a chat application. For getting frequetly comming request,messages and zone request I'm using one timer and call all methods on timer.now. The problem is that when ever I click on any control in the application this gives me a late response due to the timer running. It first hangs until it completes the timer code then control click event is fire.

So, any help on how to handle this is appreciated, I also tried threading but this didn't help.

Please give me any idea if u have.

Thanks.

TheBoyan
  • 6,802
  • 3
  • 45
  • 61
mayur Rathod
  • 1,184
  • 1
  • 11
  • 26
  • 1
    Might be helpful to post some code of the timer. – Adam Houldsworth Jul 18 '11 at 12:55
  • Do you use network realted calls in your event handlers? If so, this could be the source of high UI response times. – Emiswelt Jul 18 '11 at 13:00
  • 1
    its almost impossible to say whats going wrong there. just a wild guess - `first complete the timer code then control click event is fire.` means you are calling `timer method` on every `Page_Load` event. so post some code to reproduce the issue... – Nika G. Jul 18 '11 at 13:07
  • yes my timer is running on every 1 sec for frequntly get new data.so my resize and dragging all become running slow. – mayur Rathod Jul 18 '11 at 13:15
  • You're probably running some time consuming task or tasks in the Timer event handler. Could you post some code so that we can see? – TheBoyan Jul 18 '11 at 13:34

1 Answers1

3

Use System.Timers.Timer or System.Threading.Timer instead of the Windows.Windows.Forms.Timer, and inside the Elapced event handler whenever you call methods or properties on UI controls use control.InvokeRequired and control.Invoke.

the problem with the form timer is that it perform the action on UI thread, From msdn:

Windows timer is designed for a single-threaded environment where UI threads are used to perform processing

Edit: Here is example using System.Timers.Timer:

private System.Timers.Timer _chatTimer = new System.Timers.Timer();

public Form1()
{
    InitializeComponents();

    _chatTimer.Interval = 1000;//1 seconds
    _chatTimer.Elapsed += OnChatTimerElapsed;
    _chatTimer.AutoReset = true;
}

private void OnChatTimerElapsed(object sender, System.Timer.ElapsedEventArts e)
{
    //code to perform when timer elapsed.
}

Edit2: Another thing to notice that depending on execution time on the elapsed event handler, if the time required to execute the code on it is larger than 1 second then I suggest you to set _chatTimer.AutoReset to false and only start the timer after the previous elapsed event is finished. for example check this.

Community
  • 1
  • 1
Jalal Said
  • 15,906
  • 7
  • 45
  • 68
  • u are right friend.but we are not aware of control.invokeRequired and control.invoke method. can u explain this with example? – mayur Rathod Jul 18 '11 at 13:09
  • ok thans friend we are trying this if any problem then again post comment. – mayur Rathod Jul 18 '11 at 13:17
  • hello friend again i have a problem with this solutions,this will not update treeview control node.please help for this with one control invoke method – mayur Rathod Jul 18 '11 at 13:39
  • @mayur: that's why I introduce "`control.InvokeRequired` and `control.Invoke`" in the first place. when you are inside the timer elapsed method, you are in a different thread rather than the thread that created the UI object "the treeview". but you can't update UI object from another thread. so whenever you want to interact with the UI object from another thread. check `InvokeRequired` and use `Invoke` to perform the action on it. for more info check [this](http://stackoverflow.com/questions/6556330/threading-cross-threading-in-c-net-how-do-i-change-combobox-data-from-another/6556401#6556401). – Jalal Said Jul 18 '11 at 13:48
  • by using invoke i can't change treenode image,please send me proper example. – mayur Rathod Jul 18 '11 at 14:07
  • @mayur: only inovke when interacting with the treeview `OnChatTimerElapsed(object sender, ElapsedEventArgs e) { string data = GetNewData(); /*other code that does not interact with treeview*/ if (treeview.InvokeRequired) { treeview.Invoke(new MethodInvoker(() => UpdateTreeView(data)));} else { UpdateTreeView(data);} }` – Jalal Said Jul 18 '11 at 14:41
  • @mayur: you are welcome. don't forget to mark the answer as the accepted answer if it solves your problem. – Jalal Said Jul 18 '11 at 14:48
  • this is working fine bit when i create form object and show the form using from.show method at that time generate error that null referance of form so how i invoke form same as treeview u mention early. – mayur Rathod Jul 19 '11 at 06:12