Reactive Extensions: Why does this exit immediately? - system.reactive

I am reading IntroToRx and I'm having a bit of trouble with the sample code. Here is the sum total of my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace LearningReactiveExtensions
{
public class Program
{
static void Main(string[] args)
{
var observable = Observable.Interval(TimeSpan.FromSeconds(5));
observable.Subscribe(
Console.WriteLine,
() => Console.WriteLine("Completed")
);
Console.WriteLine("Done");
Console.ReadKey();
}
}
}
If I understand the book correctly, this should write a sequence of numbers to the console, once every five seconds forever since I never Dispose() of the sequence.
However, when I run the code, all I get is the "Done" at the end. No numbers, no "completed", nothing but "Done".
What am I doing wrong here?

I am assuming you haven't had the patience to wait for 5 seconds to elapse otherwise you would have seen that the code is working.
The main idea to keep in mind with Rx is that Observable.Subscribe will return control to calling method almost immediately. In other words, Observable.Subscribe does not block until the results are produced. Thus the call to Console.WriteLine will be invoked only after five seconds.

You need some way to make the main thread wait for what you are doing. You can use a semaphore if you like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace LearningReactiveExtensions
{
public class Program
{
static void Main(string[] args)
{
SemaphoreSlim ss = new SemaphoreSlim(1);
var observable = Observable.Interval(TimeSpan.FromSeconds(5));
observable.Subscribe(
Console.WriteLine,
() => {
Console.WriteLine("Completed");
ss.Release();
}
);
ss.Wait();
Console.WriteLine("Done");
Console.ReadKey();
}
}
}
Though probably better in this case just to write
static void Main(string[] args)
{
SemaphoreSlim ss = new SemaphoreSlim(1);
Observable.Interval(TimeSpan.FromSeconds(5)).Wait();
Console.WriteLine("Completed");
Console.WriteLine("Done");
Console.ReadKey();
}

Related

Speech to text enabling in unity not working

I am using unity 2018.1.0f2 and windows 10. Below is the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Windows.Speech;
public class Dinovoice : MonoBehaviour {
KeywordRecognizer KeywordRecognizerObj;
void Start () {
string[] keywords_array={"Jump","Run"};
KeywordRecognizerObj= new KeywordRecognizer(keywords_array);
KeywordRecognizerObj.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
KeywordRecognizerObj.Start();
Debug.Log("Started");
}
void Update() {
}
private void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
Debug.Log("Ended");
}
}
When I run from unity Started is getting printed.Ended is not getting printed at all when I speak.I am using unity with wikitude. I am adding this script to my prefab.Can you please help me out.Is there any setting I am missing?
Thanks

Text update with sliders not working correctly

I have 10 sliders that have text components attatched to them, they are supposed to display the slider values and save the values to playerprefs. This all works perfectly except that some of the text boxes will not update/display their text when the scene is played again. Half of the text boxes populate their text values with their saved value from playerprefs, the other half return null, even though they're value is being saved properly.
here is my code to save the values (attached to each slider):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveSliderValue : MonoBehaviour {
public Slider Slider;
public float valueofslider;
void Start()
{
valueofslider = PlayerPrefs.GetFloat(gameObject.name + "valueofslider");
Slider.value = valueofslider;
}
void Update()
{
valueofslider = Slider.value;
if (Input.GetKeyDown(KeyCode.S))
{
PlayerPrefs.SetFloat(gameObject.name + "valueofslider", valueofslider);
Debug.Log("save");
}
}
}
and to display the values (attached to each text component)::
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class showvalue : MonoBehaviour {
Text percentageText;
// Use this for initialization
void Start () {
percentageText = GetComponent<Text>();
Debug.Log(percentageText);
}
// Update is called once per frame
public void textUpdate (float value)
{
if (percentageText != null)
percentageText.text = value.ToString();
else
Debug.Log("Variable percentagetext is not set.");
}
}
and the error:
Variable percentagetext is not set.
UnityEngine.Debug:Log(Object)
showvalue:textUpdate(Single) (at Assets/showvalue.cs:24)
UnityEngine.UI.Slider:set_value(Single)
SaveSliderValue:Start() (at Assets/SaveSliderValue.cs:19)
pictures - for understanding
and if I remove the debug.log I get a null reference.
The order of execution for Start functions can't be relied upon. Rename the showvalue Start function to Awake and see if it helps.
Basically what happens is:
some of showvalue instances execute their Start function earlier than their corresponding SaveSliderValue
thus they set the value of the text correctly
and for some that order is broken (because Start functions execute in arbitrary order) => your error
Awake is always executed before Start - use that to your advantage.
working code for the ShowValues script here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class showvalue : MonoBehaviour {
Text percentageText;
// Use this for initialization
void Awake () {
percentageText = GetComponent<Text>();
//percentageText = GameObject.Find("ThePlayer").GetComponent<SaveSliderValue>().valueofslider;
}
// Update is called once per frame
public void textUpdate (float value)
{
if (percentageText != null)
percentageText.text = value.ToString();
else
Debug.Log("Variable percentagetext is not set.");
}
}

How do you add notifcationhubs library

How do you include notificationhubs library to azure function? This is my attempt and it doesn't work
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Azure.NotificationHubs;
public static void Run(string myQueueItem, TraceWriter log)
{
log.Info($"C# Queue trigger function processed: {myQueueItem}");
Notification a;
}
We'll add NotificationHubs to the list of built in assemblies, but for now you can add a package reference to NoficationHubs by adding a project.json file for your Function (as described in the documentation here).
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Azure.NotificationHubs": "1.0.5"
}
}
}
}
With that in place you can add a using statement for NotificationHubs, e.g.:
using System.Net;
using Microsoft.Azure.NotificationHubs;
public static HttpResponseMessage Run(
HttpRequestMessage req,
TraceWriter log,
out Notification notification)
{
log.Info($"C# HTTP trigger function RequestUri={req.RequestUri}");
// TODO
notification = null;
return req.CreateResponse(HttpStatusCode.OK);
}

Stream is not writable when i try use a StreamReader constructed by the same fileStream?

As code after, i don't want create fileStream twice, so i write streamWriter inside fileStream. But it was so strange that code throw an exception when try to instance a StreamWriter. I guess fs's read pointer reach at the end, but it's just a guess. I wanna know why meet this error. Please help me!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Stream
{
class Program
{
static void Main(string[] args)
{
string fileName = "E:\\test.txt";
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (StreamReader r = new StreamReader(fs)) {
}
using (StreamWriter w = new StreamWriter(fs)) {
//exception when new StreamWriter(fs)
//stream is not writable. why? why? why?
}
Console.ReadKey();
}
}
}
}
when finished using(StreamReader r = new StreamReader(fs)), code will invoke fs's close() automatically. So when next using fs, there is a check in StreamWriter's constructor: if(steam ==null ){throw new ArgumentException("stream is not writable")}. There it is!

Unity TCP Server Hang Unity scene

I am trying to implement a TCP server in unity. I am using unity pro 3.5 and when I run this code in a scene, then unity hang, no response at all untill I kill it with Task Manager.
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System.Text;
public class Server : MonoBehaviour {
private IPAddress ipAd;
public string IP="127.0.0.1";
public int port = 8001;
private Socket s;
void Update ()
{
}
// Use this for initialization
void Awake () {
port = 8001;
ipAd = IPAddress.Parse(IP);
msg = "Listening at " + IP + ":" + port.ToString();
this.s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.s.Bind(new IPEndPoint(ipAd,port));
this.s.Listen(200);
while (true)
this.ReceiveMessage(this.s.Accept()); //hang if this line activated
}
private void ReceiveMessage(Socket socket)
{
byte[] tempbuffer = new byte[10000];
socket.Receive(tempbuffer);
rec.AssignFromByteArray(tempbuffer);
}
}
Server should run in it's own thread so the GameLoop can continue even if tcp_Listener.AcceptSocket() is a blocking call.
using UnityEngine;
using System.Collections;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
public class Server : MonoBehaviour {
private bool mRunning;
public static string msg = "";
public Thread mThread;
public TcpListener tcp_Listener = null;
void Awake() {
mRunning = true;
ThreadStart ts = new ThreadStart(Receive);
mThread = new Thread(ts);
mThread.Start();
print("Thread done...");
}
public void stopListening() {
mRunning = false;
}
void Receive() {
tcp_Listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8001);
tcp_Listener.Start();
print("Server Start");
while (mRunning)
{
// check if new connections are pending, if not, be nice and sleep 100ms
if (!tcp_Listener.Pending()){
Thread.Sleep(100);
}
else {
Socket ss = tcp_Listener.AcceptSocket();
BonePos rec = new BonePos();
byte[] tempbuffer = new byte[10000];
ss.Receive(tempbuffer); // received byte array from client
rec.AssignFromByteArray(tempbuffer); // my own datatype
}
}
}
void Update() {
}
void OnApplicationQuit() { // stop listening thread
stopListening();// wait for listening thread to terminate (max. 500ms)
mThread.Join(500);
}
}
See Related answer from answers.unity3d.com
You get the hanging because of this:
while (true)
this.ReceiveMessage(this.s.Accept()); //hang if this line activated
The entire process is stuck in this loop, so nothing else gets processed.
What you could do (though I would not recommend it) is to perform the action inside update() instead of an infinite loop.
what I suggest is to use something else to run your server instead of unity.
If you are creating a game server, you can use PUN (Photon Unity Networking) or write a standalone server in C#, or even an open-sourced server in python that matches your needs.
You have to get rid of this statement:
while (true)
this.ReceiveMessage(this.s.Accept());
And your server has to run in his own thread.
Unity's engine is single threaded, and the life cycle methods of monobehaviours run synchronously in a predefined order. An infinite loop in one life cycle method will infinitely hang the program.
Coroutines are one way that you can create infinite loops without hanging indefinitely. This use case would likely make use of WaitForSeconds