Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have a textbox , through textbox i am doing chat and this textbox is continuously being used hence it has blocked my program UI other controls . So i want to make a thread for my textbox event . So that i would not block my program UI . Consider below as a textbox event for which i want to make a thread.

private void textBox1_KeyDown(object sender,KeyEventArgs e)
{

}

So how its Possible ??? I want to do it in C# .Net ..... Thanks ...
Posted
Comments
Sergey Alexandrovich Kryukov 3-Aug-15 1:31am    
What does it mean, make a thread for an event? Do you mean starting a thread in a KeyDown event handler. Certainly, but it hardly can make any practical sense. So many threads? Why? Better tell us what do you want to achieve.
—SA
I want to start a thread for the text_Box_Event , because my this text box event is continuosly being used until program is closed , so this text_box_event blocks the UI . Thats why i want to make a thread and attach a event with a thread....
Ralf Meier 3-Aug-15 2:15am    
In my opinion it is nearly impossible to make a Suggestion with this Information you gave.
But I agree with Sergey : in the Moment it makes no sense to have a Thread beside the KeyDown-Event-Handler. For what ?
My Suggestion is this : give us more info about your programm-functionality and what happens what you don't want to have.
What is meant with : "it blocks the UI" ? Normally a correct Event-handling could not block the UI ...
Sergey Alexandrovich Kryukov 3-Aug-15 2:51am    
It does not mean to make any sense. If you really have such a problem, create a minimal but 100% comprehensive self-contained code and show it using "Improve question".
—SA
himanshu agarwal 3-Aug-15 5:00am    
If using textbox is making your application freeze, then I would like to know what other event handlers have you hooked on to on that text box?

Adding, another event handler will only degrade the performance.

You shouldn't do that. You should instead create a worker thread for the chat communication and pass data to such worker thread from your text box (see "How to: Make Thread-Safe Calls to Windows Forms Controls"[^]).
 
Share this answer
 
Comments
F-ES Sitecore 5-Aug-15 4:59am    
+5d The trick the OP is missing is that he needs to call "Invoke" so the update is executed on the UI thread. I'd recommend not using a textbox for the whole chat though, maybe maintain a label or some other text area, and just have a textbox for writing out messages.

I would recommend to use a thread pool, to queue your logic of sending data to server.


C#
ThreadPool.QueueUserWorkItem(SendData, "data");

then, you may define the SendData function with the logic you wrote in the KeyDown handler.


C#
public void SendData(object inputParam)
{
  try
  {
    string input = inputParam as string;
    writer.Write(Form1.GetSysName() + " : " + input); // Send to Sever
    ...
  }
  catch (SocketException se)
  {
    output_text.Text += "\n Error waiting Object";
  }
}

However, if you want your messages to be sent in synchronized way then, threadpool will not be a good choice, there you may just spawn a thread.


C#
Thread th = new Thread(SendData);
th.Start();

But, you may also need to define a mechanism to keep queuing your requests.

 
Share this answer
 

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