By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.


P: n/a
Hi, Im using a timer every 1 second to update a datagridview, but Im
haveing problems getting it to run on a seperate thread. At the moment
the form freezes when the thread runs, I think its because im firing
the thread with in the timer, Ive tried fireing the timer from thread
start. This is my trimmed code;
private void timer1_Tick(object sender, EventArgs e)
Thread thread = new Thread(new ThreadStart(liveprices));
thread.IsBackground = true;
public delegate void onliveprices();
private void liveprices()
if (InvokeRequired)
BeginInvoke(new onliveprices(liveprices));
puts data into dateset and adds new rows to table
adds table to datagridview


I need to run liveprices every second, and I need to resize/move the
form. At the momment I cant because of freezing.
I tried Thread thread = new Thread(new ThreadStart(timer.start);
thread.IsBackground = true;
thread.Start(); but that froze all together.
RegardS Robert

Feb 13 '08 #1
Share this Question
Share on Google+
5 Replies

P: n/a
OK; the timer (via SyncContext) fires on the UI thread. You then start
a thread, which immediately does a BeginInvoke back to the UI

At the moment you background thread is doing nothing useful; try to
split it so that it does something like:

private void liveprices()
{ // expecting this on a non-UI thread!
//TODO: go get the data into a new DataTable, array or
BeginInvoke((MethodInvoker) delegate {
//TODO: update the grid

Now the "fetch" happends on the worker thread, and the UI update
happens on the UI thread.

However, every second seems optimistic for DataTable; I'm hoping
you're using a "get changes since <x>" mechanism in the database?

And note that if your datatable is UI bound you can only touch it from
the UI thread.

Feb 13 '08 #2

P: n/a
Thanks for your reply, thats improved peformance. I dont get complete
freeze. I still get it for a split second and im guessing this is
because the updates on the ui thread. My grids not linked to a
database, Im fetching data from the internet and updating the grid.
Ive no need to retain the data in a database.
Regards Robert
Feb 13 '08 #3

P: n/a
Thanks Nicholas, Threading is complicated. Ok, Am I runderstanding
this correctly then, what I was doing was calling my thread to do all
the work, on the UI thread. When infact I should have done my work
first, then called the thread to update the UI. In which case does
this meen in general, when you preform tasks such as
calculations,fetching etc these are on seperate thread to the UI
until such time you invoke thread. Sorry I hope that makes sense.
Regards Robert
Feb 13 '08 #4

P: n/a
Thanks for taking the time to explain. Ive been doing it the wrong way
round, which explains why ive had problems with other programs. Still
its fun. Thanks again.
Regards Robert
Feb 13 '08 #5

P: n/a
Thanks for your replys, Im reading about system.threading.timer along
with threading period. Its takes some getting into.
Regards Robert
Feb 14 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.