Search Unity

System.Timers cross platform issue WP8

Discussion in 'Scripting' started by Jockey4her, May 24, 2015.

  1. Jockey4her

    Jockey4her

    Joined:
    Apr 3, 2013
    Posts:
    12
    I wrote a working project which used System.Timers class. This worked fine on Windows, but when I did a build for Windows Phone, I got a build error saying System.Timers was not found in the framework.

    Reference Rewriter: Error: type `System.Timers.Timer` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void codebehind::Update().
    UnityEngine.Debug:LogError(Object)
    PostProcessWP8Player:RewriteReferencesForAssembly(String, String, String, String, String, String, String) (at C:/buildslave/unity/build/PlatformDependent/WP8Player/Extensions/Managed/PostProcessWP8Player.cs:450)
    PostProcessWP8Player:MakeManagedAssembliesWp8Compatible(String, String, LibraryCollection) (at C:/buildslave/unity/build/PlatformDependent/WP8Player/Extensions/Managed/PostProcessWP8Player.cs:411)
    PostProcessWP8Player:postProcess(BuildTarget, BuildOptions, String, String, String, String, String, Guid) (at C:/buildslave/unity/build/PlatformDependent/WP8Player/Extensions/Managed/PostProcessWP8Player.cs:212)
    UnityEditor.HostView:OnGUI()

    MSDN shows System.Timers, but typically Windows devices prefer System.Threading to use DispatcherTimer class. In any case, I need a timers class which will work in the Windows phone. What can I use.
     
  2. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    Looking at the compatability list, it looks like System.Threading.Timer is better. However, I would be apprehensive about using .NET level functionality for this. From the docs: The method does not execute on the thread that created the timer; it executes on a ThreadPool thread supplied by the system.

    You cannot execute any Unity functionality from the non-gameplay thread. Instead, you're probably better off using Unity's Invoke / InvokeRepeating.
     
  3. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    You can use Invoke(methodName, timeInSeconds) instead of timers (also InvokeRepeating(methodName, timeInSeconds, timeBetweenInvokes).
     
  4. Jockey4her

    Jockey4her

    Joined:
    Apr 3, 2013
    Posts:
    12
    Yes Jodon, System.Threading.Timers is closer to a real solution, thanks. The problem is that Unity3D does not have a System.Timer code class that is compatible with the Windows Phone. That's a cross-compatibility issue which is going to hurt a lot of apps that rely on it.

    The idea of writing my own timer class by hand and choking out the already sluggish main loop, when there are perfectly good feature rich event driven timer classes that have been around since the stone age is not my idea of a good time.

    I ended up wrapping System.Threading.Timers in a 'looks like' class, and added the delegate hooks so the graphical thread get its slow non-atomic, updates. It builds to the Windows Phone successfully.
     
  5. sathya

    sathya

    Joined:
    Jul 30, 2012
    Posts:
    297
    Hi. I am also trying the same solution but nothing worked me so far. Can you please share the wrapper you are using. Thanks in advance