开发者

Update textbox from class in a different thread

I have been reading the realated topics to this but cant seem to get them to work.

Im trying to make an eventlog for my application. It starts by 开发者_开发技巧opening "Form1" from which I have a function that opens up "Form2". "Form2" starts a multi-threaded TcpListener. How would I edit the textbox in "Form1" from "Form2" a thread running the TcpListener. Also how would you change it from another form/class or is it done the same way.

Examples would be welcome so I can break it down and understand.

Thanks very much


Updating controls from a background thread is a common problem, and usually the simplest way to do it is to use a BackgroundWorker. Check out some of the MSDN tutorials on using a BackgroundWorker.

That works well when you're doing some background task and want to do updates during the task or at the end of the task. However, it looks like you might be doing something a bit different since you're waiting for input from a socket. In that case, it's sometimes easier to have the background thread just add any events it receives into a queue and then use a timer event to process the queue and update any controls that need updating. Just be sure to use a Windows Forms timer, and not the Threading timer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜