Threading help with Invoke

  • Thread starter Thread starter Edward J. Stembler
  • Start date Start date
E

Edward J. Stembler

I created a call-back Thread class and am getting an
Invoke error in the call-back method. The call-back
method access a control on the main thread. I understand
why the error is occurring, however, I don't know the
(Invoke?) syntax for correct it. I've played around with
MethodInvoke but cannot seem to get it to work. Has
anyone done something similar?

Here's a generic version of my code:


The call-back found in a Form:

private void worker_DataSetRetrieved(object sender,
DataSetRetrievedEventArgs e)
{
populateTreeView(e.DataSet);
}


The thread class et. al.:

internal class RequestQueueDataSetRetrieverThread
{
public event DataSetRetrievedHandler
DataSetRetrieved = null;
private string _user = "";

public RequestQueueDataSetRetrieverThread()
{
_user = Common.CurrentUser;
}

public RequestQueueDataSetRetrieverThread(string
user)
{
_user = user;
}

public void Start()
{
if (DataSetRetrieved == null)
return;

DataSet dataSet = getMyDataSet(_user);
DataSetRetrieved(this, new
DataSetRetrievedEventArgs(dataSet));
}
}

internal class DataSetRetrievedEventArgs: EventArgs
{
private DataSet _dataSet;

public DataSetRetrievedEventArgs(DataSet dataSet)
{
_dataSet = dataSet;
}

public DataSet DataSet
{
get
{
return _dataSet;
}
}
}

internal delegate void DataSetRetrievedHandler(object
sender, DataSetRetrievedEventArgs e);
 
Edward J. Stembler said:
I created a call-back Thread class and am getting an
Invoke error in the call-back method. The call-back
method access a control on the main thread. I understand
why the error is occurring, however, I don't know the
(Invoke?) syntax for correct it. I've played around with
MethodInvoke but cannot seem to get it to work. Has
anyone done something similar?

You need to use Control.Invoke to access a control from another thread.
 
Here is a helpful doc on this topic...


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/htm
l/winforms06112002.asp

_Rick
--------------------
| From: "John Saunders" <[email protected]>
| References: <[email protected]>
| Subject: Re: Threading help with Invoke
| Date: Fri, 12 Sep 2003 17:43:40 -0400
| Lines: 17
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework
| NNTP-Posting-Host: pool-151-199-57-229.bos.east.verizon.net 151.199.57.229
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:53604
| X-Tomcat-NG: microsoft.public.dotnet.framework
|
| | > I created a call-back Thread class and am getting an
| > Invoke error in the call-back method. The call-back
| > method access a control on the main thread. I understand
| > why the error is occurring, however, I don't know the
| > (Invoke?) syntax for correct it. I've played around with
| > MethodInvoke but cannot seem to get it to work. Has
| > anyone done something similar?
|
| You need to use Control.Invoke to access a control from another thread.
| --
| John Saunders
| Internet Engineer
| (e-mail address removed)
|
|
|
 
Back
Top