Exemple #1
0
 public static A Finally <A>(Fun0 <A> action, Fun0 <Unit> handler)
 {
     try
     {
         return((A)action.Apply());
     }
     finally
     {
         handler.Apply();
     }
 }
Exemple #2
0
 public static A Catch <A>(Fun0 <A> action, Fun1 <Exception, A> handler)
 {
     try
     {
         return((A)action.Apply());
     }
     catch (Exception exn)
     {
         return((A)handler.Apply(exn));
     }
 }
Exemple #3
0
    public static EventloopEntry RunBlockingPrim <A>(Fun0 <A> blockingAction, Fun2 <Exception, A, Unit> onSuccess)
    {
        EventloopEntry entry = GetEventloopEntry();

        ThreadPool.QueueUserWorkItem((object info) => {
            Exception exception = null;
            A result            = default(A);
            try {
                result = blockingAction.Call();
            }
            catch (Exception exn) {
                exception = exn;
            }
            entry.Post(() => { onSuccess.Call(exception, result); });
        });
        return(entry);
    }
Exemple #4
0
 public ThreadTimer(Fun0 <Unit> cb, int ms)
 {
     entry = Primitive.GetEventloopEntry();
     if (ms <= 0)
     {
         timer = null;
         entry.Post(() => { cb.Apply(); });
     }
     else
     {
         timer = new Timer((object state0) => {
             if (entry != null)
             {
                 entry.Post(() => { cb.Apply(); });
             }
         }, null, ms, Timeout.Infinite);
     }
 }
Exemple #5
0
    // For now, the MainConsole enters an event loop that handles
    // ReadLine from the Console. Later other asynchronous api's can
    // be added through the AsyncGlobal class, keeping the application
    // active as long as there are 'on' handlers installed.
    public static A MainConsole <A>(Fun0 <A> f)
    {
        A x = (A)f.Apply();

        while (!AsyncGlobal.AllDone())
        {
            if (onReadLine != null)
            {
                string         s   = Console.In.ReadLine();
                Async <string> res = onReadLine;
                onReadLine = null;
                res.Supply(s); // this may set onReadLine again
            }
            else
            {
                // bad
                Primitive.Trace("MainConsole: active async's but no readline");
                break;
            }
        }
        return(x);
    }
Exemple #6
0
    // For now, the MainConsole enters an event loop that handles
    // ReadLine from the Console. Later other asynchronous api's can
    // be added through the AsyncGlobal class, keeping the application
    // active as long as there are 'on' handlers installed.
    public static A MainConsole <A>(Fun0 <A> f)
    {
        A x = (A)f.Apply();

        while (activeEntries > 0 || work.Count > 0)
        {
            Action action;
            lock (workMutex) {
                action = (work.Count > 0 ? work.Dequeue() : null);
            }
            if (action != null)
            {
                action();
            }
            else
            {
                // wait for new work items
                // todo: we could implement efficient timers here by using timeouts
                workEvent.WaitOne();
            }
        }
        return(x);
    }
Exemple #7
0
 public static A Call <A>(this Fun0 <A> f)
 {
     return((A)f.Apply());
 }
Exemple #8
0
 public static ThreadTimer SetTimeout(Fun0 <Unit> cb, int ms)
 {
     return(new ThreadTimer(cb, ms));
 }