Windows窗体冻结c# [英] windows form is freezing c#

查看:90
本文介绍了Windows窗体冻结c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

this is my code:

namespace JoystickSample
{

   
    public partial class frmMain : Form
    {
        
        private JoystickInterface.Joystick jst;
        string RxString;
        public frmMain()
        {
            InitializeComponent();
            
        }

    
        private void frmMain_Load(object sender, EventArgs e)
        {
            // grab the joystick
            jst = new JoystickInterface.Joystick(this.Handle);
            string[] sticks = jst.FindJoysticks();
            jst.AcquireJoystick(sticks[0]);

            // add the axis controls to the axis container
            for (int i = 0; i < jst.AxisCount; i++)
            {
                Axis ax = new Axis();
                ax.AxisId = i + 1;
                flpAxes.Controls.Add(ax);
            }

            // add the button controls to the button container
            for (int i = 0; i < jst.Buttons.Length; i++)
            {
                JoystickSample.Button btn = new Button();
                btn.ButtonId = i + 1;
                btn.ButtonStatus = jst.Buttons[i];
                flpButtons.Controls.Add(btn);
            }

            // start updating positions
            tmrUpdateStick.Enabled = true;
        }

        private void tmrUpdateStick_Tick(object sender, EventArgs e)
        {
            // get status
            jst.UpdateStatus();

            // update the axes positions
            foreach (Control ax in flpAxes.Controls)
            {
                if (ax is Axis)
                {
                    switch (((Axis)ax).AxisId)
                    {
                        case 1:
                            ((Axis)ax).AxisPos = jst.AxisA;
                            
                            break;
                        case 2:
                            ((Axis)ax).AxisPos = jst.AxisB;

                            break;
                        case 3:
                            ((Axis)ax).AxisPos = jst.AxisC;
                            break;
                        case 4:
                            ((Axis)ax).AxisPos = jst.AxisD;
                            break;
                        case 5:
                            ((Axis)ax).AxisPos = jst.AxisE;
                            break;
                        case 6:
                            ((Axis)ax).AxisPos = jst.AxisF;
                            break;
                    }                
                 
                }
            }

            // update each button status
            foreach (Control btn in flpButtons.Controls)
            {
                if (btn is JoystickSample.Button)
                {
                    ((JoystickSample.Button)btn).ButtonStatus =
                        jst.Buttons[((JoystickSample.Button)btn).ButtonId - 1];
                }
            }
        }

        private void flpAxes_Paint(object sender, PaintEventArgs e)
        {

        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            SerialPort.PortName = "COM4";
            SerialPort.BaudRate = 9600;

            SerialPort.Open();
           if (SerialPort.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                timerSending.Start();
               
            }
        }
        
        private void buttonStop_Click(object sender, EventArgs e)
        {
            if (SerialPort.IsOpen)
            {
                SerialPort.Close();
                buttonStart.Enabled = true;
                buttonStop.Enabled = false;
                timerSending.Stop();

            }
        }

       
            public void slanje(object sender, EventArgs e)
            {
                if (!SerialPort.IsOpen) return;
                //slanje AxisPos=jst.AxisA(ili možda axisA?) kao vrijednost položaja analoga?

                string strAxisA = Convert.ToString(jst.AxisA);
                SerialPort.Write( "AxisA=" + strAxisA);
          
                string strAxisB = Convert.ToString(jst.AxisB);
                SerialPort.Write("AxisB=" + strAxisB);
        
                string strAxisC = Convert.ToString(jst.AxisC);
                SerialPort.Write("AxisC=" + strAxisC);
        
                string strAxisD = Convert.ToString(jst.AxisD);
                SerialPort.Write("AxisD=" + strAxisD);
        
            }

            private void timerSending_Tick(object sender, EventArgs e)
            {
                slanje(null, null);
                                         
            }

            private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                
                    RxString = SerialPort.ReadExisting();
                    this.Invoke(new EventHandler(DisplayText));
              
            }
            private void DisplayText(object sender, EventArgs e)
            {
                textBox1.AppendText(RxString);
            }
           
    }
}







所以我发送操纵杆轴位置真正的串口到我的文本框,当我通过单击停止按钮关闭端口时出现问题,并且没有发送信息。它应该像这个AxisA:32454一样写,如果我点击停止它结束在AxisA上写字:3,没有2454,表格冻结。如何避免这种情况。 sry for bad eng。




so im sending the joystick axis position true serial port to my textbox, and problem occures when i close the port by clicking the stop button, and the messege is not send all the way. it should be written like this AxisA:32454, and if i hit stop and it ends writting on AxisA:3, without 2454, form freeze. how to avoid that. sry for bad eng.

推荐答案

 public static bool canStop;
 public bool CanStop {get { return canStop; } }

   // update each button status
try{
     foreach (Control btn in flpButtons.Controls)
     {
        canStop = false;
        if (btn is JoystickSample.Button)
        {
           ((JoystickSample.Button)btn).ButtonStatus =
               jst.Buttons[((JoystickSample.Button)btn).ButtonId - 1];
        }
     }
   }finally {canStop = true;}





编辑添加评论按钮:





Edit to add button from comments:

private void buttonStop_Click(object sender, EventArgs e)
{
   if(CanStop == true)
   {
      if (SerialPort.IsOpen)
      {
      freeze();    // Wtf is this ??
      SerialPort.Close();
      buttonStart.Enabled = true;
      buttonStop.Enabled = false;
      timerSending.Stop();
      }
   }
   else
     {
       MessageBox.Show("Not finished");
     }
}

















这个非常粗糙,但你想做这样的事情然后单击停止时检查CanStop是真还是假。



基本上,你在Foreach循环结束前停止。



此外,您的应用程序可能正在挂起,因为您在更新UI时没有调用。









This is pretty rough, But you want to do something like this and then check if "CanStop" is true or false when you click Stop.

Basically, You're stopping before the Foreach loop finishes.

Also, Your application is probably hanging because you aren't invoking when you update your UI.


这篇关于Windows窗体冻结c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆