Re: Socket is freezing my datagrid
- From: "edge" <superedge@xxxxxxxxxxxxxxx>
- Date: Sat, 13 Aug 2005 06:55:09 +0800
"Mehdi" <vioccc@xxxxxxxxxxxxxxxxxx> wrote in message
news:nzy6h1zgit7i$.1vrf7cnfybbyr.dlg@xxxxxxxxxxxxx
> 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.
Great hints!
Well, seems that it is working, but instead of calling the new MethodInvoker
I am using a delegate. Please look at how i am doing:
in my ClientForm.cs I have this class:
public class MyCallbackClass : RemotelyDelegatableObject
{
private ClientForm mainChild = null;
public MyCallbackClass (ClientForm pMain) { mainChild = pMain ; }
protected override void InternalSubmissionCallback (object sender,
RemoteEventArgs e)
{
try
{
//mainChild is my client form, UpdateClient is the workerThread
//I think here is the secret. I must call a threadsafe method to update
my grid.
//FYI, I was doing mainChild.AddRowToDataGrid( e.MyStringData ) and it
was freezing
mainChild.UpdateMyClient( e.MyStringData );
}
catch( Exception ex )
{
//mainChild.listbox2.Items.Add(
ex.Message+"-"+ex.Source+"-"+ex.InnerException.Message );
}
}
and my ClientForm has this:
public void UpdateMyClient( string data )
{
// I created a delegate called SetIncomingDataTextDelegate
this.Invoke( new SetIncomingDataTextDelegate( AddRowToDataGrid ), new
object[] { data } );
// AddRowToDataGrid inserts the data string feed into the datagrid
}
a few more questions:
Is this the way it should be? is it a valid alternative?
Also I noticed that the client form is acting a little slow when I
drag-and-move it in my screen. maybe because the listening?
at first I was trying to use BeginInvoke() with no success, looks like the
correct way it is Inkove();
TIA
-Edge
.
- Follow-Ups:
- Re: Socket is freezing my datagrid
- From: Mehdi
- Re: Socket is freezing my datagrid
- References:
- Re: Socket is freezing my datagrid
- From: edge
- Re: Socket is freezing my datagrid
- From: Mehdi
- Re: Socket is freezing my datagrid
- Prev by Date: Re: Socket is freezing my datagrid
- Next by Date: Re: What about CAO?
- Previous by thread: Re: Socket is freezing my datagrid
- Next by thread: Re: Socket is freezing my datagrid
- Index(es):
Relevant Pages
|