每当我在C#中收到传入消息时,为什么不会运行它?

时间:2017-08-29 12:01:32

标签: c# event-handling task streamreader streamwriter

我测试了这段代码,在Monodevelop上放置一个断点,在Raspbian上运行。此应用程序需要使用C#控制台应用程序与Windows通信。 虽然timer1_Tick方法每次打印传入的消息(并且它清楚地打印新消息,因为我在传入消息后每次都清除消息变量),似乎OnNewData不会每次都运行,当时&# 39;是传入的消息。 我通过发送" mes"来询问新收到的消息。在timer1_Tick中输出。 我不会在该方法中处理其他类型的传入消息,但是对于" mes"的响应,这个地方最适合处理。原因是" mes"创建答案类型("","键入答案),我可以随时获得。 我不知道是什么问题。任何的想法?如果在每个新的传入消息之后正好刷新文本框,这怎么可能发生?

public partial class Form0 : Form
{

    public static string ReadCharactersIntoString(){
        char c='c';
        string outputString=String.Empty;
        do{
            c=(char)streamReader.Read();
            outputString=outputString+c;
        }while(c!='>');
        return outputString;
    }
    async void OnNewData(object sender, EventArgs e){
        message = Form0.ReadCharactersIntoString ();

        //work with message variable works unreliable...
        //sometimes this method runs, when there's an incoming message, sometimes it's not...
        //although tick method prints the message everytime...
        //why? how to solve this?

        reseter.Set ();
    }
    public void AcceptMessages(){
        while (true) {
            try{
            if (streamReader!=null && streamReader.Peek () > 0) {
                newIncomingDataEvent.Invoke(this,EventArgs.Empty);
                reseter.WaitOne ();
            }
            }catch{

            }
        }
    }
    public static async Task Waiting(){
        while (message == null || message[message.Length-1]!='>') {
        }
    }
    public static async Task<string> CollectingAnswer(){
        await Waiting();

        return message;
        //when the answer is completely collected, it gives me back
    }
    public void ConnectionEstablisher()
    {
        while (true)
        {
            if (!activeConnection)
            {
                try
                {    
                    socketForServer = new TcpClient(ip, port);
                    networkStream = socketForServer.GetStream();
                    streamReader = new System.IO.StreamReader(networkStream);
                    streamWriter = new System.IO.StreamWriter(networkStream);
                    streamReader.BaseStream.ReadTimeout = 5000;
                    streamWriter.BaseStream.ReadTimeout = 5000;                      
                    activeConnection = true;

                    newIncomingDataEvent+=OnNewData;
                    reseter = new ManualResetEvent (true);
                }
                catch(Exception e)
                {
                    activeConnection = false;                      
                }
            }
        }
    }

    private async void timer1_Tick(object sender, EventArgs e)
    {
        if (this.Visible && activeConnection)
        {
            try
            {
                streamWriter.Write("mes");
                streamWriter.Flush();
                textBoxSending.AppendText("mes" + Environment.NewLine);

                collectAnswer=CollectingAnswer();
                outputString=await collectAnswer;
                message=null;

                textBoxAccepting.AppendText(outputString + Environment.NewLine);

                //this always prints the incoming response message...

0 个答案:

没有答案