473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

timer/threading

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;
thread.Start();
}
public delegate void onliveprices();
private void liveprices()
{
if (InvokeRequired)
{
BeginInvoke(new onliveprices(liveprices));
}
else
{
dowork
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
5 1890
OK; the timer (via SyncContext) fires on the UI thread. You then start
a thread, which immediately does a BeginInvoke back to the UI
thread...

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
whatever
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.

Marc
Feb 13 '08 #2
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: andrewcw | last post by:
I am trying to do a windows service with C#. I am using as a base the VB.NET article in VS, but I thing the WITHEVENTS timer notation is a delegate. Can anyone provide sample code & anh hints. ...
3
by: ELO | last post by:
Hi all Every week, I need to get two files on a remote server. I have developped a C# Windows Service with two System.Threading.Timer to do this task For the first one, the delay (TimeSpan...
2
by: hnkien | last post by:
Hi, I am writing a windows service with threading.timer for 10 seconds but it didn't work. Here are my code: namespace SchedulerService { public class ScheduleService :...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
5
by: Dave | last post by:
I trying to setup a time. The following code is in a function, Timer stateTimer = new Timer(ab(), null, 1000, 1000); I have included using System.Threading; at the begining of my code....
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
6
by: whtinkm | last post by:
Hi, All Recently, my project need some code like following: using System; using System.Threading; namespace MyTimerTest { class Class1 {
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
11
by: Anil Gupte/iCinema.com | last post by:
When I use this Dim instance As New Timer I get the error: Error 1 Overload resolution failed because no accessible 'New' accepts this number of arguments. Yet, in the help section for...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.