fawad
fawad

Reputation: 1353

Timer call in C# stops into to a Textbox

I'm working on chat software in C# 2010 where all incoming messages are being received by a fixed timer. When I type any message for sending to the user and during my sentence typing if incoming message timer call comes everything stops for few seconds and then resume again.

Is there any way to make timer and input to textbox independent ?

Upvotes: 1

Views: 446

Answers (5)

Jens H
Jens H

Reputation: 4632

Although the already posted suggestions all are directed to the same problem, the actual cause of your application hangs was not directly mentioned here yet.

This is, simply put, too much work in the same thread.

In your case, the timer event is handled on the UI thread and causes some CPU load and therefore the input in the UI controls slows down because the precious CPU time is taken away to whatever happens in the TimerTick event handling method.

The solution therefore is to use multiple threads.

If you need to keep the timer architecture instead of using the MSMQ - which I would still strongly suggest - I recommend you to this MSDN article: Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads

The article is 'a few days old', but the principles remain absolutely valid today.

If you are working in a .NET 4.x version, you can also use the Task Parallel Library to make working with multiple threads easier.

The upcoming .NET 4.5 also offers the even more comfortable await and asyc keywords: Asynchronous Programming with Async and Await.

Upvotes: 0

Nighil
Nighil

Reputation: 4129

if you are using winforms then try to get message in seperate thread

Thread thread = new Thread(new ThreadStart(GetMessage));
thread.Start();

public void GetMessage()
{
     //Message showing code goes here 
      Thread.Sleep(10000);
      GetMessage();
}

Upvotes: 0

General Grey
General Grey

Reputation: 3688

You don't need the timer independent, you need what the timer does to be.

Inside your timer.Tick event add this

Task.Factory.StartNew( () =>
{
  //do what you need to do here ie.
      string something ="Hello";

      this.BeginInvoke( new Action(() =>
         {
             //update textbox here
             textBox1.Text=something;
         }));

 });

You will need this namespace

using System.Threading.Tasks;

Upvotes: 0

Jens H
Jens H

Reputation: 4632

Thinking about it, a timer might not be the best solution.

I suggest using some kind messaging system. A windows built-in technology to be used with .NET would be the Microsoft Message Queue.

There are existing frameworks that sit on top of the MSMQ to make working with it more comfortable, like NServiceBus or the open source MassTransit.

This would decouple receiving from your UI thread and enable you for a more responsive UI on the client application.

Upvotes: 1

Sudhakar B
Sudhakar B

Reputation: 1563

Instead of System.Forms.Timer use System.Timers.Timer. System.Forms.Timer will run in the UI thread where as System.Timers.Timer runs in a different thread.

Upvotes: 2

Related Questions