C# unable to modify variables from event - c#

I am working with C# in Unity 5. I ported some TCP code over from an app I built using .NET 4.5, based heavily on this:
Asynchronous Socket Communication
I am unable to call any methods upon receiving data in the server app.
I receive TCP communication on an event:
public void OnReceivedData(IAsyncResult ar)
{
SocketChatClient client = (SocketChatClient)ar.AsyncState;
byte[] aryRet = client.GetRecievedData(ar);
string aryRetString = System.Text.Encoding.ASCII.GetString(aryRet);
ParseCommand(aryRetString);
if (aryRet.Length < 1)
{
client.Sock.Close();
tcpClients.Remove(client);
return;
}
client.SetupReceiveCallback(this);
}
ParseCommand() is very simple (for testing):
private void ParseCommand(string cmdToParse)
{
string[] splitInput = cmdToParse.Split(',');
testValue++;
Debug.Log(testValue.ToString());
int playerStation = 0;
}
When I use Debug.Log() to output the test value class variable to the console from with ParseCommand, it increments properly each time I call ParseCommand. However, if I continuously output testValue outside of ParseCommand, it never increments. Also, if I call a method inside of ParseCommand, the method does not fire, and it behaves as though it has exceptioned although no exception is thrown -- i.e., the method stops on that line and the calling method will not fire again.

Related

C# - Send input to child process - console app

Background info
I am writing an integration test that spawns a child process (c# console app). The test is counting some rows in the database after the process is spun up and after the process is closed. The process is closed via process.Kill()
When the process is killed in this manner, it doesn't hit the Stop method within the process. I need to call this stop method to stop threads and remove entries from the database in order for the test to pass.
Original Code
The console app process that I am spawning in my test:
static void Main(string[] args)
{
TaskManager tm = new TaskManagerProcess();
if (Environment.UserInteractive ||
(args.EmptyForNull().Any(a => a.Equals("-RunInteractive", StringComparison.OrdinalIgnoreCase) || a.Equals("/RunInteractive"))))
{
tm.ConsoleStart(args);
Console.WriteLine("Press [Enter] to shut down, any other key to mark");
while (true)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
break;
Console.WriteLine("========================================================");
Console.Out.Flush();
}
Console.WriteLine("Shutting down...");
tm.ConsoleStop();
}
else
{
ServiceBase.Run(tm);
}
}
}
The test code:
//count before starting child proc
int preCount;
//count after process is spun up
int runningsCount;
//count after stopped
int postCount;
//Get an initial count of the logged in modules before svc host is started
user = ApiMethod.GetLoggedInUsers().Where(x => x.RecId == userRecID).FirstOrDefault();
preCount = user.LoggedInModules.Count;
Process proc = Helper.StartProcess(ConnectionBundle);
//Give process time to spin up leaders and workers
await Task.Delay(TimeSpan.FromSeconds(30));
//Get a count of modules after process is spun up
user = ApiMethod.GetLoggedInUsers().Where(x => x.RecId == userRecID).FirstOrDefault();
runningCount = user.LoggedInModules.Count;
//Write a line terminator to the child svc host process -
//this allows it to shutdown normally
Helper.ProcessInput.WriteLine();
Helper.ProcessInput.Close();
Helper.KillProcess(proc);
await Task.Delay(TimeSpan.FromSeconds(5));
//Get count of logged in modules after process is closed
user = ApiMethod.GetLoggedInUsers().Where(x => x.RecId == userRecID).FirstOrDefault();
postCount = user.LoggedInModules.Count;
Helper is a static class that sets up the process start info(including args) and starts the process. In helper I've redirected the StandardInput and added a property ProcessInput which is set to the StandardInput of the created process.
My goal is to send input of "Enter" from the test to the spawned process so that it will break from the loop and call tm.ConsoleStop()
TaskManagerProcess is a private custom class that controls the process. It does not inherit from System.Diagnostics.Process. As an alternate approach, my test could interact with TaskManagerProcess directly. However, I can't make TaskManagerProcess public and I need to run TaskManagerProcess in its own AppDomain because calling ConsoleStop is disposing objects in the API that I need to finish the test.
Things I've Tried
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(CloseProcDelgate handler, bool add);
I tried adding a call to Kernel32.SetConsoleCtrlHandler (and the necessary delegate) to call ConsoleStop when the process is exited. This doesn't seem to work when the process is killed via process.Kill()
With the original process code, I noticed an exception when I wrote to the StandardInput. The exception message told me to use Console.Read instead of Console.ReadKey(). This actually works intermittently! I can sometimes get a breakpoint on int cKey = Console.Read() (with debugger attached to child process) but other times it doesn't hit the breakpoint.
while (true)
{
//Changing this to Console.Read instead of Console.ReadKey
//Allows us to send redirected input to process?
int cKey = Console.Read();
if ((ConsoleKey)cKey == ConsoleKey.Enter)
break;
Console.WriteLine("========================================================");
Console.Out.Flush();
}
Finally, I tried interacting with TaskManagerProcess directly. I made the private class internal, and marked the internals visible to my test assembly. I cannot make the class public.
When I go this route, calling tm.ConsoleStop() blows away some objects in my API so I can't check the count after this method is called. For this reason, I thought I would create a new AppDomain and call AppDomain.CreateInstanceAndUnwrap() on the TaskManagerProcess class. However, I get an exception here, I believe its due to the the fact that the class is internal.
I am really stuck at this point! Any help is appreciated and thanks for taking the time to read this!
Edit
I created a demo project here
that shows what I am trying to do and has both approaches in the Test method.
Initially I thought I couldn't call AppDomain.CreateInstanceAndUnwrap() because the TaskManagerProcess class was internal. However, after playing with my demo project, I think I just can't load the assembly.
I'm guessing here, but I believe your TaskManagerProcess is a service application. If it is not, please ignore this. If it is, be advised of including details like this in your question. Debugging service applications can be complicated, believe me, I've been there. But before proceed, more advise.
Test the methods in your modules, no whole running programs, as Michael Randall just said.
Unless absolutely necessary, don't do tests against a database. Mock whatever you need to test your code.
You should go back to your alternate approach of interact with TaskManagerProcess directly. From the code of your console app, the only working method I see called is tm.ConsoleStart(args), the rest inside the loop is console writing and reading. So you can't change the acces level of that class, again, I've been there. What I have done in the past to overcome this is to use conditional compilation to create a kind of public facade in my private or internal modules.
Suppose you have:
internal class TaskManagerContainer
{
private class TaskManagerProcess
{
internal void Start()
{
// stuff
}
private void DoSomething(int arg)
{
// more stuff
}
}
}
Change it like this:
#define TEST
// Symbol TEST can also be defined using the GUI of your IDE or compiler /define option
internal class TaskManagerContainer
{
//
#if TEST
public class TaskManagerProcess
#else
private class TaskManagerProcess
#endif
{
internal void Start()
{
// stuff
}
private void DoSomething(int arg)
{
// more stuff
}
#region Methods Facade for Testing
#if TEST
public void Start_Test()
{
Start();
}
private void DoSomething_Test(int arg)
{
DoSomething(arg);
}
#endif
#endregion
}
}
I really hope it will help you making the methods visible to the test assembly and it won't blow objects in you API.
I think I got it with a brute force approach.
while (!testProcess.HasExited)
{
testProcess.StandardInput.WriteLine();
}
Thanks everyone for the input!

TNonblockingServerTransport not implemented with Thrift C# library

When using Apache Thrift [https://github.com/apache/thrift] to create a non blocking server in C#, the following Classes/Types cannot be recognized:
TNonblockingServerTransport
TNonblockingServer
I want to send command from my win10 laptop to control a time-consuming calculation performed on a high performance server (ubuntu). That's why I came to Apache Thrift. I have found the official C# version tutorial [https://github.com/apache/thrift/tree/master/tutorial/csharp] and it works well. This tutorial uses the so-called Blocking Mode (TSimpleServer). But in my situation, the time-consuming calculation procedure should be interrupt-able. Consequently, I must use a non-blocking server.
The logic is simple. For the server, I used a private flag forceStop. If the Client call Stop(), forceStop will set to true and the calculation loop will break.
// #Server#
// Server Set-Up
private void SetUp()
{
try
{
CalculatorHandler handler = new CalculatorHandler();
Calculator.Processor processor = new
Calculator.Processor(handler);
var serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(processor, serverTransport);
// Use this for a multithreaded server
// server = new TThreadPoolServer(processor, serverTransport);
Console.WriteLine("Starting the server...");
server.Serve();
}
catch (Exception x)
{
Console.WriteLine(x.StackTrace);
}
}
private bool forceStop;
public int TimeConsumingOperation(int n1, int n2)
{
Console.WriteLine("add({0},{1})", n1, n2);
for (int i = 0; i < 10; i++)
{
//calculating
Thread.Sleep(500);
if (forceStop)
{
Quit();
}
}
return n1 + n2;
}
public void Stop()
{
forceStop = true;
}
// Client
// Button#1 Click callback
private void Button_Start_Click()
{
client.TimeConsumingOperation(0,0);
}
// Button#2 Click callback
private void Button_Stop_Click()
{
client.Stop();
}
//
I've found some useful examples in java [https://chamibuddhika.wordpress.com/2011/10/02/apache-thrift-quickstart-tutorial/]. I've try my best to convert the java code of non-block server to the corresponding C# code but I found that there seems to be no TNonblockingServerTransport in C#. Anyone could help me with this probelm?
// Java Code
public class NonblockingServer {
private void start() {
try {
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
ArithmeticService.Processor processor = new ArithmeticService.Processor(new ArithmeticServiceImpl());
TServer server = new TNonblockingServer(new TNonblockingServer.Args(serverTransport).
processor(processor));
System.out.println("Starting server on port 7911 ...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
NonblockingServer srv = new NonblockingServer();
srv.start();
}
}
There's actually two answers to that question.
Possible implementation
Your setup is flawed, to begin with.
// #Server#
// Server Set-Up
private bool forceStop;
public void Stop()
{
forceStop = true;
}
Let's assume, we have two clients, both starting a new calculation. Now one client wants to abort. What happens?
The solution would be to structure it in a way where the calculation is a separate business logic object that is instantiated at TimeConsumingOperation() and made available to the client by some means, typically by returning some sort of ID.
When the client now wants to abort, it calls Stop(calcualtionID). The server side logic now routes that call to the implementation and triggers whatever the abort mechanism might be, with C# is is probably a CancellationToken.
A third call would be needed to query the final results from the server end once the calculation has finished. Note that we still work with a TSimpleServer and the reason it works is because we avoid blocking calls by means of the API design.
Nonblocking server
Yes, there is no implementation yet for C#. Since Thrift is Open Source, that probably simply means that there were nobody running into that use case so far and wanted to spend time on an implementation. That is not to say that such a use case may not exist.
What exists are
Threaded and Threadpool servers
Task.Run(() => { your code })
which may help to solve your use case. Also, when used with ASP.NET there is no need for a nonblocking server since the runtime offers enough support already for multiple connections.
Bottom line
There are certain ways to work around that limitation you experierenced. One additional alternative could be to become a contributor by porting one of the existing (e.g. Java) nonblocking implementation to NetStd (preferred, since C# and NetCore will mature into "deprecated" state with the next release and both be replaced by NetStd eventually)

Receiving only different values from serial port

I have a project like a registration system using RFID. When user enabled registration mode, each card shown should be saved only once. For this, I am using an array and then checking the same code in array to see if it exists already. But I have a problem with incrementing the array index.
in class Form1, i have the initializations:
string rx_data = "";
string last_data = "";
string[] availablePlayers = {""};
int plIndex = 0;
In the code below, if I use plIndex, it receives only the first card and seems like it stops calling this handler again.
public void WriteRxData(object sender, SerialDataReceivedEventArgs e)
{
if (connection.IsOpen && !cardSaveCon.IsOpen)
{
try
{
rx_data = connection.ReadLine(); // check how the data ends
if (!availablePlayers.Any(rx_data.Contains))
{
availablePlayers[plIndex] = rx_data;
receivedData.AppendText(rx_data);
plIndex++;
}
}
catch (Exception err)
{
connection.Close();
}
}
But instead, If I use a hardcoded index value, it works. I would like to know how to handle this since this had to work for other languages. I am new to c#, so there may be some parts that I am missing.
availablePlayers[plIndex] = rx_data;
This will crash your code the second time you receive a string. Unfortunately you are also catching the IndexOutOfRangeException and close the port. Which will completely deadlock the code, SerialPort.Close() can only complete when the event handler has returned.
Specific counter-measures:
use a List<string> instead of a string[]
remove try/catch from your code, it cannot ever do anything but make your program fail without a way to recover
write an event handler for AppDomain.CurrentDomain.UnhandledException to provide a diagnostic when your program dies on unexpected exceptions
get familiar with the Debug + Windows + Threads debugger window. It allows you to see what's going on in other threads, you would have seen the deadlock.

How to fix this Speech Recognition wicked bug?

I have this code in my C# project:
public void startRecognition(string pName)
{
presentationName = pName;
if (WaveNative.waveInGetNumDevs() > 0)
{
string grammar = System.Environment.GetEnvironmentVariable("PUBLIC") + "\\SoundLog\\Presentations\\" + presentationName + "\\SpeechRecognition\\soundlog.cfg";
if (File.Exists(grammar))
{
File.Delete(grammar);
}
executeCommand();
/// Create an instance of SpSharedRecoContextClass which will be used
/// to interface with the incoming audio stream
recContext = new SpSharedRecoContextClass();
// Create the grammar object
recContext.CreateGrammar(1, out recGrammar);
//recContext.CreateGrammar(2, out recGrammar2);
// Set up dictation mode
//recGrammar2.SetDictationState(SpeechLib.SPRULESTATE.SPRS_ACTIVE);
//recGrammar2.SetGrammarState(SPGRAMMARSTATE.SPGS_ENABLED);
// Set appropriate grammar mode
if (File.Exists(grammar))
{
recGrammar.LoadCmdFromFile(grammar, SPLOADOPTIONS.SPLO_STATIC);
//recGrammar.SetDictationState(SpeechLib.SPRULESTATE.SPRS_INACTIVE);
recGrammar.SetGrammarState(SPGRAMMARSTATE.SPGS_ENABLED);
recGrammar.SetRuleIdState(0, SPRULESTATE.SPRS_ACTIVE);
}
/// Bind a callback to the recognition event which will be invoked
/// When a dictated phrase has been recognised.
recContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(handleRecognition);
// System.Windows.Forms.MessageBox.Show(recContext.ToString());
// gramática compilada
}
}
private static void handleRecognition(int StreamNumber,
object StreamPosition,
SpeechLib.SpeechRecognitionType RecognitionType,
SpeechLib.ISpeechRecoResult Result)
{
string temp = Result.PhraseInfo.GetText(0, -1, true);
_recognizedText = "";
// System.Windows.Forms.MessageBox.Show(temp);
// System.Windows.Forms.MessageBox.Show(recognizedWords.Count.ToString());
foreach (string word in recognizedWords)
{
if (temp.Contains(word))
{
// System.Windows.Forms.MessageBox.Show("yes");
_recognizedText = word;
}
}
}
This codes generates a dll that I use in another application.
Now, the wicked bug:
- when I run the startRecognition method in the beginning of the execution of the other application, this codes works very well. But when I run it some time after the beginning, this codes works but the handleRecognition method is never called. I see that the words are recognized because they appear on the Microsoft Speech Recognition app, but the handler method is never called.
Do you know what's the problem with this code?
NOTE: this project has some code that is allways being executed. Might that be the problem? Because the other code is running it doesn't allow it to this to run?
It could be that in the second call to startRecognition() an exception is being thrown before the handler can be added to recContext.Recognition. Put a try/catch around the everything in startRecognition(), and echo any exceptions that get thrown.
I would also output the value of WaveNative.waveInGetNumDevs() to a log or trace file. If it is not > 0 the startRecognition() method won't do anything.
I had another handler in another part of the code.
The recognition handler had to be called before the other one.
I made that way and it worked :)

C# Fire and Forget call inside a WebMethod

We have a C# WebMethod that is called synchronously by a Delphi CGI (don't ask!). This works fine except when we switch to our disaster recovery environment, which runs a lot slower. The problem is that the Delphi WinInet web request has a timeout of 30 seconds, which cannot be altered due a Microsoft-acknowledged bug. In the disaster recovery environment, the C# WebMethod can take longer than 30 seconds, and the Delphi CGI falls flat on its face.
We have now coded the C# WebMethod to recognise the environment it is in, and if it is in disaster recovery mode then we call the subsequent method in a thread and immediately respond to the CGI so that it is well within the 30 seconds. This makes sense in theory, but we are finding that these threaded calls are erratic and are not executing 100% of the time. We get about a 70% success rate.
This is clearly unacceptable and we have to get it to 100%. The threads are being called with Delegate.BeginInvoke(), which we have used successfully in other contexts, but they don't like this for some reason.... there is obviously no EndInvoke(), because we need to respond immediately to the CGI and that's the end of the WebMethod.
Here is a simplified version of the WebMethod:
[WebMethod]
public string NewBusiness(string myParam)
{
if (InDisasterMode())
{
// Thread the standard method call
MethodDelegate myMethodDelegate = new MethodDelegate(ProcessNewBusiness);
myMethodDelegate.BeginInvoke(myParam, null, null);
// Return 'ok' to caller immediately
return 'ok';
}
else
{
// Call standard method synchronously to get result
return ProcessNewBusiness(myParam);
}
}
Is there some reason that this kind of 'fire and forget' call would fail if being used in a WebService WebMethod environment? If so then is there an alternative?
Unfortunately altering the Delphi side is not an option for us - the solution must be in the C# side.
Any help you could provide would be much appreciated.
Do you try to use the "HttpContext" in your method? If so, you should store it in a local variable first... also, I'd just use ThreadPool.QueueUserWorkItem.
Example:
[WebMethod]
public string NewBusiness(string myParam)
{
if (InDisasterMode())
{
// Only if you actually need this...
HttpContext context = HttpContext.Current;
// Thread the standard method call
ThreadPool.QueueUserWorkItem(delegate
{
HttpContext.Current = context;
ProcessNewBusiness(myParam);
});
return 'ok';
}
else
{
// Call standard method synchronously to get result
return ProcessNewBusiness(myParam);
}
}
As the documentation says, EndInvoke should be always called, so you have to create a helper for doing FireAndForget operations like this one:
http://www.reflectionit.nl/Blog/default.aspx?guid=ec2011f9-7e8a-4d7d-8507-84837480092f
I paste the code:
public class AsyncHelper {
delegate void DynamicInvokeShimProc(Delegate d, object[] args);
static DynamicInvokeShimProc dynamicInvokeShim = new
DynamicInvokeShimProc(DynamicInvokeShim);
static AsyncCallback dynamicInvokeDone = new
AsyncCallback(DynamicInvokeDone);
public static void FireAndForget(Delegate d, params object[] args) {
dynamicInvokeShim.BeginInvoke(d, args, dynamicInvokeDone, null);
}
static void DynamicInvokeShim(Delegate d, object[] args) {
DynamicInvoke(args);
}
static void DynamicInvokeDone(IAsyncResult ar) {
dynamicInvokeShim.EndInvoke(ar);
}
}
We use this code successfully in our application, although it is not web.

Categories

Resources