A
Andy Baker
I have recently upgraded a VS.NET 2003/CF 1.1 project to VS.NET 2005 and am
getting the error 'System.NotSupportedException - Control.Invoke must be
used to interact with controls created on a separate thread', on my CE.NET 5
device. It happens after I have transferred data from a SQL Server database.
I have a class to do this that uses a TCPClient object to establish a
connection then it logs into the database and uses SqlClient to transfer
data. When the transfer is completed, I raise an event to say that it is
completed. It is in the code that handles the completed event that the error
occurs.
I understand what the error message is saying and it looks like I have
unitentionally created a separate thread for the connection, and the
controls have to be updated from the main program thread. I tried using
Control.Invoke and it did allow me to do what I wanted, but was very slow -
I have seen this mentioned on the net about similar problems. Therefore I
think there are two possible solutions. 1) Do everything in one thread - as
far as I can see there is no reason for the app to be multithreaded at this
point as it cannot do anything else until the data transfer is complete. 2)
Use the main program thread to update the user interface controls. As I have
not intentionally created the thread, how can I tell when it has closed and
when control has passed back to the main thread? My connection class
implements IDisposable and in the Dispose sub I have closed the TCPClient,
but after disposing of the connection object I cannot see what to do next.
Any suggestions would be appreciated.
Andy Baker
getting the error 'System.NotSupportedException - Control.Invoke must be
used to interact with controls created on a separate thread', on my CE.NET 5
device. It happens after I have transferred data from a SQL Server database.
I have a class to do this that uses a TCPClient object to establish a
connection then it logs into the database and uses SqlClient to transfer
data. When the transfer is completed, I raise an event to say that it is
completed. It is in the code that handles the completed event that the error
occurs.
I understand what the error message is saying and it looks like I have
unitentionally created a separate thread for the connection, and the
controls have to be updated from the main program thread. I tried using
Control.Invoke and it did allow me to do what I wanted, but was very slow -
I have seen this mentioned on the net about similar problems. Therefore I
think there are two possible solutions. 1) Do everything in one thread - as
far as I can see there is no reason for the app to be multithreaded at this
point as it cannot do anything else until the data transfer is complete. 2)
Use the main program thread to update the user interface controls. As I have
not intentionally created the thread, how can I tell when it has closed and
when control has passed back to the main thread? My connection class
implements IDisposable and in the Dispose sub I have closed the TCPClient,
but after disposing of the connection object I cannot see what to do next.
Any suggestions would be appreciated.
Andy Baker