timer/threading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RobcPettit@yahoo.co.uk

    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(obj ect sender, EventArgs e)
    {
    Thread thread = new Thread(new ThreadStart(liv eprices));
    thread.IsBackgr ound = true;
    thread.Start();
    }
    public delegate void onliveprices();
    private void liveprices()
    {
    if (InvokeRequired )
    {
    BeginInvoke(new onliveprices(li veprices));
    }
    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(tim er.start);
    thread.IsBackgr ound = true;
    thread.Start(); but that froze all together.
    RegardS Robert

  • Marc Gravell

    #2
    Re: timer/threading

    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((Me thodInvoker) 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

    Comment

    • RobcPettit@yahoo.co.uk

      #3
      Re: timer/threading

      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

      Comment

      • RobcPettit@yahoo.co.uk

        #4
        Re: timer/threading

        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,fe tching etc these are on seperate thread to the UI
        until such time you invoke thread. Sorry I hope that makes sense.
        Regards Robert

        Comment

        • RobcPettit@yahoo.co.uk

          #5
          Re: timer/threading

          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

          Comment

          • RobcPettit@yahoo.co.uk

            #6
            Re: timer/threading

            Thanks for your replys, Im reading about system.threadin g.timer along
            with threading period. Its takes some getting into.
            Regards Robert

            Comment

            Working...