NotSupportedException

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
 
G

Guest

I've not seen any issue with Control.Invoke being slow. It simply posts a
message back to the main thread. The only reason that would be slow would
be starvation and if that's the case moving it to the primary thread won't
help - the processor will still be saturated. Unless you app is doing a lot
of stuff I'd be inclined to think you have a logic problem somewhere causing
the perceived slowness.
 
A

Andy Baker

To be honest, I haven't had chance to look at it much yet. It updated the
control as I wanted it to, but then didn't appear to carry on to the next
line. I will look at it again when I have more time - is there not an easy
way to prevent a new thread from starting when I don't need it to?

Andy Baker
 
G

Guest

Well if you're making a call that underneath creates a thread (like an async
call) then no, there's no way to prevent it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top