Re: Socket is freezing my datagrid
- From: Mehdi <vioccc@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 12 Aug 2005 21:05:02 +0100
On Fri, 12 Aug 2005 11:18:28 +0800, edge wrote:
> I do not know about this worker thread.
> Do I have to follow any guideline to update my UI control?
> any sample code that I can see?
>
> and as far as I know I am not implementing any thread in my app. Please
> advice.
There are 3 situation where your code can be executed within a worker
thread:
1) you manually create a new Thread and start it. This is apparently not
what you are doing
2) you are queuing your methods in the threadpool queue so that they get
executed in one of the ThreadPool thread. This is probably not what you are
doing either since you would know that you are using threads if you had
done that
3) you are starting an asynchronous operation and supplying a callback
method that gets executed once the asynchronous operation has completed.
This callback is always executed within a worker thread. The Socket class
provides such methods (BeginConnect, BeginSend, BeginReceive...). If you
are using them, then you are in a multi-threaded situation.
In order to safely update your datagrid from a worker thread, you should
marshall the call to the method that accesses your datagrid to the UI
thread (the thread where all the UI controls of your application have been
created). You can do that by using either the Invoke or BeginInvoke method
of the Control class.
Say that you have a control called uiControl (this can be anything, your
form, your datagrid, anything as long as it derives from the Control
class). Here is the code to do what you want safely:
void UpdateDatagrid()
{
// Here do whatever you want
// with your Datagrid
}
void WorkerThreadMethod()
{
// This is the method executing
// in a worker thread where
// you want to update the datagrid
// Marshall the call to UpdateDatagrid() to the
// UI thread
uiControl.Invoke(new MethodInvoker(UpdateDatagrid));
}
One important thing here is that the Invoke method relies on uiControl to
have a valid Handle in order to properly marshall the call to the UI
thread. So you should make sure that uiControl has a valid handle. You can
be sure that uiControl has a valid Handle if it has been displayed on the
screen.
.
- Follow-Ups:
- Re: Socket is freezing my datagrid
- From: edge
- Re: Socket is freezing my datagrid
- References:
- Re: Socket is freezing my datagrid
- From: edge
- Re: Socket is freezing my datagrid
- Prev by Date: Avoiding "Server Busy" Dialog
- Next by Date: Re: Socket is freezing my datagrid
- Previous by thread: Re: Socket is freezing my datagrid
- Next by thread: Re: Socket is freezing my datagrid
- Index(es):
Relevant Pages
|