Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Thread sychronization

Thread sychronization

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koleraba
    wrote on last edited by
    #1

    I created a custom timer class (MyTimer). The class is basicaly just a wrapper that internaly uses System.Threading.Timer. Class MyTimer will be used in a windows service app. Since the Timer class signals it events on the background thread, I am using Dispatcher.Invoke to marshal the requests to the current thread. Below is the event handler for the internaly used System.Threading.Timer.

    private void timer_Elapsed(object state)
    {
    if (Dispatcher.CurrentDispatcher != _dispatcher)
    // _dispatcher is a reference to the Dispatcher under wich the Timer was instantated
    {
    _dispatcher.Invoke(
    new System.Threading.ThreadStart(delegate
    {
    onTick();
    }), DispatcherPriority.Send);
    }
    else
    {
    onTick();
    }
    }

    private void onTick()
    {
    if (Tick != null)
    {
    Tick(this, new EventArgs());
    }
    }

    The problem is that in some casses the _dispatcher.Thread is in stopped or WaitSleepJoin state, and the onTick() method doesn't execute. Does anybody has a solution to this problem? Any ideas will be appreciated. Uroš

    modified on Friday, February 12, 2010 11:29 AM

    N 1 Reply Last reply
    0
    • K koleraba

      I created a custom timer class (MyTimer). The class is basicaly just a wrapper that internaly uses System.Threading.Timer. Class MyTimer will be used in a windows service app. Since the Timer class signals it events on the background thread, I am using Dispatcher.Invoke to marshal the requests to the current thread. Below is the event handler for the internaly used System.Threading.Timer.

      private void timer_Elapsed(object state)
      {
      if (Dispatcher.CurrentDispatcher != _dispatcher)
      // _dispatcher is a reference to the Dispatcher under wich the Timer was instantated
      {
      _dispatcher.Invoke(
      new System.Threading.ThreadStart(delegate
      {
      onTick();
      }), DispatcherPriority.Send);
      }
      else
      {
      onTick();
      }
      }

      private void onTick()
      {
      if (Tick != null)
      {
      Tick(this, new EventArgs());
      }
      }

      The problem is that in some casses the _dispatcher.Thread is in stopped or WaitSleepJoin state, and the onTick() method doesn't execute. Does anybody has a solution to this problem? Any ideas will be appreciated. Uroš

      modified on Friday, February 12, 2010 11:29 AM

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Please read this[^] Look at #3, "Hi" is not a descriptive subject line and is why your question is being ignored.


      I know the language. I've read a book. - _Madmatt

      K 1 Reply Last reply
      0
      • N Not Active

        Please read this[^] Look at #3, "Hi" is not a descriptive subject line and is why your question is being ignored.


        I know the language. I've read a book. - _Madmatt

        K Offline
        K Offline
        koleraba
        wrote on last edited by
        #3

        Thanks for the warning, it was a honest mistake. Uros

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups