tracy1421 2021-07-08 11:51 采纳率: 55.6%
浏览 36
已结题

调用控件一段时间出现 error 28 out of stack space

最近在做C#手写输入功能,在网上找了个demo,用microsoft.ink.dll来做的,我把这个控件设计好加入到自己的控件中,运行一段时间就会报out of stack space 的错,想请教各位是哪里出了问题,或者如何能定位到问题,手写板控件定义相关代码如下

public partial class HandWrite : Control
    {
        public static FrmMain _FrmMain; //自己的主面板
        InkCollector ic;
        RecognizerContext rct;
        string FullCACText;
        public HandWrite()
        {
            InitializeComponent();
            ic = new InkCollector(ink_here.Handle);
            this.ic.Stroke += new InkCollectorStrokeEventHandler(ic_Stroke);
            ic.Enabled = true;
            ink_();
 
            this.rct.RecognitionWithAlternates += new RecognizerContextRecognitionWithAlternatesEventHandler(rct_RecognitionWithAlternates);
            //单字识别
            rct.RecognitionFlags = RecognitionModes.WordMode;
            rct.Strokes = ic.Ink.Strokes;
        }
 
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
 
       
        void rct_RecognitionWithAlternates(object sender, RecognizerContextRecognitionWithAlternatesEventArgs e)
        {
 
            string ResultString = "";
            RecognitionAlternates alts;
 
            if (e.RecognitionStatus == RecognitionStatus.NoError)
            {
                alts = e.Result.GetAlternatesFromSelection();
                Console.WriteLine(alts.Count);
                for (int i = 0; i < alts.Count && i < 5; i++)   // 取前5个识别结果
                {
                    RecognitionAlternate alt = alts[i];
                    ResultString += alt.ToString() + " ";
                }
                //foreach (RecognitionAlternate alt in alts)
                //{
                //    ResultString = ResultString + alt.ToString() + " ";
                //}
            }
            FullCACText = ResultString.Trim();
            Console.WriteLine(FullCACText);
            Control.CheckForIllegalCrossThreadCalls = false;
            GetString(FullCACText);
            Control.CheckForIllegalCrossThreadCalls = true;
 
        }
 
        private void GetString(string txt)
        {
            string[] str_temp = txt.Split(' ');
            string str_temp1 = "shibie_";
            string str_temp2 = "";
            if (str_temp.Length > 0)
            {
                for (int i = 0; i < str_temp.Length; i++)
                {
                    str_temp2 = str_temp1 + i.ToString();
                    Control[] con_temp = tlpBox.Controls.Find(str_temp2, true);
                    if (con_temp.Length > 0)
                    {
                        ((Button)(con_temp[0])).Text = str_temp[i];
                        //Label lab = (Label)(con_temp[0]);
                        //lab. = str_temp[i];
 
                    }
                }
            }
        }
 
        void ic_Stroke(object sender, InkCollectorStrokeEventArgs e)
        {
            rct.StopBackgroundRecognition();
            rct.Strokes.Add(e.Stroke);
            rct.BackgroundRecognizeWithAlternates(0);
        }
 
        private void ink_()
        {
            Recognizers recos = new Recognizers();
            Recognizer chineseReco = recos.GetDefaultRecognizer();
 
            rct = chineseReco.CreateRecognizerContext();
        }
 
        private void btn_shibie_Click(object sender, EventArgs e)
        {
            #region 单字识别处理
            List<Point> lstPoints = new List<Point>();
 
            foreach (var stroke in ic.Ink.Strokes)
            {
                lstPoints.AddRange(stroke.GetPoints());
            }
            ic.Ink.DeleteStrokes();//清除手写区域笔画;
            Stroke sk = ic.Ink.CreateStroke(lstPoints.ToArray());
            rct.Strokes.Add(sk);
            #endregion
            //_FrmMain.tbKeyboardInput.Text= ic.Ink.Strokes.ToString();            
            //textBox1.SelectedText = ic.Ink.Strokes.ToString();
            FrmMain._FrmMain.tbKeyboardInput.SelectedText = ic.Ink.Strokes.ToString();
        }
 
        private void handTimer_Tick(object sender, EventArgs e)
        {
            btn_shibie.PerformClick();
            if (!ic.CollectingInk)
            {
                Strokes strokesToDelete = ic.Ink.Strokes;
                rct.StopBackgroundRecognition();
                ic.Ink.DeleteStrokes(strokesToDelete);
                rct.Strokes = ic.Ink.Strokes;
                ic.Ink.DeleteStrokes();//清除手写区域笔画;
                ink_here.Refresh();//刷新手写区域
            }
            handTimer.Stop();
        }
 
 
        private void ink_here_MouseUp(object sender, MouseEventArgs e)
        {
            handTimer.Enabled = true;
        }
 
        private void ink_here_MouseDown(object sender, MouseEventArgs e)
        {
            handTimer.Enabled = false;
        }
    }

调用如下


//定义子窗口变量     
public HandWrite hw;      
 
 
 
 
//初始化
 
             internal void InitHandWrting(Control control, string strLInput, string strLTip, bool isPassword)
        {
            // 解决单击tlp ImeMode失效
            control.Focus();
 
            hw = new HandWrite();
            hw.Name = "handwrite";
            hw.Dock = DockStyle.Fill;
     
            tlpKeyboard.Controls.Add(hw);
            //hw.Hand_Load();
 
            ParamControl = control;
            gbKeyboardResult.Text = strLInput + ":" + strLTip;
 
            tbKeyboardInput.Multiline = !isPassword;
            tbKeyboardInput.UseSystemPasswordChar = isPassword;
 
            //tbKeyboardInput.ImeMode = control.ImeMode;
            tbKeyboardInput.Text = control.Text;
        }
 
 
 
 
//调用
 
  private void tbNewTestName_Click(object sender, EventArgs e)
        {
            
            FrmMain._FrmMain.InitHandWrting(tbNewTestName,Properties.Resources.InfoInput_Title_NewTestName, Properties.Resources.InfoInput_Info_NewTestName, false);
            FrmMain._FrmMain.ShowPKeyboard();
        }


  • 写回答

2条回答 默认 最新

  • 关注

    报out of stack space 的错误,原因是控件里面的变量申请了资源没有释放,导致时间长了内存不足。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月17日
  • 已采纳回答 3月9日

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?