线程自动停止 [英] thread stop automatically

查看:74
本文介绍了线程自动停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在我的项目中使用了两个线程,有时候它不能正常工作。

一段时间它开始并显示我想要的结果但是在执行程序停止期间。没有错误消息。在输出窗口中显示此消息两次或三次线程< no name =>'(某些内存地址)已退出,代码为0(0x0)。程序代码请在下面给出。

任何人都可以指导我。 (请尽可能使用代码)请不要感恩。



hi all,
i am using two threads in my project, some time it works sometime not.
some time it starts and show the results what i want but during execution program stops. no error message. while shows in the output window this message two or three times "The thread '<no name="">' (some memory address) has exited with code 0 (0x0). " code of the program is given below please.
anyone can guide me please. ( if possible with code) ill be thankful please.

 public void FindNext()
        {

            while (true)
            {               
                
                Point fromCellPoint;
                Point toCellPoint;
                int nextLocation = utilObject.SelectNextCell(startCell, rnd);
                if (nextLocation  <= 0 || nextLocation  > 100)
                {
                    nextLocation = utilObject.SelectNextCell(startCell, rnd);
                }                
                    fromCellPoint = Get_CellCenterPoint(startCell);
                    toCellPoint = Get_CellCenterPoint(nextLocation);
                    string s = startCell.ToString() + " , " + nextLocation.ToString() ;
                    this.Invoke((MethodInvoker)delegate { listBox1.Items.Add(s); });     
                    moveCircleThread = new Thread(() => moveCircle(startCell, nextLocation,                fromCellPoint, toCellPoint, 100));
                    moveCircleThread.Start();
                    moveCircleThread.Join();                  
                    startCell = nextLocation;
            }
        }

public int moveCircle(int fromCell, int toCell, Point startPoint, Point endPoint,int speed)
        {
            int ismoved=0;

            while ((startPoint.X != endPoint.X) || (startPoint.Y != endPoint.Y))
            {
                int motionDirection = FindDirection(startPoint, endPoint);

                #region Switch

                switch (motionDirection)
                {
                    case 1:
                        //event for Direction1 (North)                       

                        if ((startPoint.Y - endPoint.Y) < y_moveUnit   && (startPoint.X == endPoint.X))
                            startPoint.Y = endPoint.Y;
                        else
                            startPoint.Y -= y_moveUnit  ;
                       
                        DrawCircle(startPoint,Sim_Nov_13 .Properties .Resources .mouseNorth);

                        break;

                    case 2:
                        //event for Direction2 (NE)                        

                        if (Math.Abs(startPoint.X - endPoint.X) < x_moveUnit )
                            startPoint.X = endPoint.X;
                        else if ((startPoint.Y - endPoint.Y) < y_moveUnit  )
                            startPoint.Y = endPoint.Y;
                        else
                        {

                            startPoint.X += x_moveUnit ;
                            startPoint.Y -= y_moveUnit;
                        }

                        DrawCircle(startPoint, Sim_Nov_13.Properties.Resources.mouseNorthEast );

                        break;

                    case 3:
                        //event for Direction3 (East)

                        
                        if (Math.Abs(startPoint.X - endPoint.X) < x_moveUnit && (startPoint.Y == endPoint.Y))
                            startPoint.X = endPoint.X;
                        else
                            startPoint.X += x_moveUnit;

                        DrawCircle(startPoint, Sim_Nov_13.Properties.Resources.mouseEast);

                        break;


                    case 4:
                        //event for Direction4 ( SE)

                        
                        if (Math.Abs(startPoint.X - endPoint.X) < x_moveUnit && (startPoint.Y == endPoint.Y))
                            startPoint.X = endPoint.X;
                        else if (Math.Abs(startPoint.Y - endPoint.Y) < y_moveUnit)
                            startPoint.Y = endPoint.Y;
                        else
                        {
                            startPoint.X += x_moveUnit;
                            startPoint.Y += y_moveUnit;
                        }
                        DrawCircle(startPoint, Sim_Nov_13.Properties.Resources.mouseSouthEast );

                        break;

                    case 5:
                        //event for Direction5 (South)
                       

                        if (Math.Abs(startPoint.Y - endPoint.Y) < y_moveUnit && (startPoint.X == endPoint.X))
                            startPoint.Y = endPoint.Y;
                        else
                            startPoint.Y += y_moveUnit;

                        DrawCircle(startPoint, Sim_Nov_13.Properties.Resources.mouseSouth );

                        break;

                    case 6:

                        

                        //event for Direction6 (SW)

                        if (Math.Abs(startPoint.X - endPoint.X) < x_moveUnit)
                            startPoint.X = endPoint.X;
                        if (Math.Abs(startPoint.Y - endPoint.Y) < y_moveUnit)
                            startPoint.Y = endPoint.Y;
                        else
                        {
                            startPoint.X -= x_moveUnit;
                            startPoint.Y += y_moveUnit;
                        }
                        DrawCircle(startPoint, Sim_Nov_13.Properties.Resources.mouseSouthWest);

                        break;

                    case 7:
                        //event for Direction7 (West)
                       

                        if (Math.Abs(startPoint.X - endPoint.X) < x_moveUnit)
                            startPoint.X = endPoint.X;
                        else
                            startPoint.X -= y_moveUnit;

                        DrawCircle(startPoint, Sim_Nov_13.Properties.Resources.mouseWest);

                        break;


                    case 8:
                        //event for Direction8 (NW)

                        
                        if (Math.Abs(startPoint.X - endPoint.X) < x_moveUnit)
                            startPoint.X = endPoint.X;
                        else if (Math.Abs(startPoint.Y - endPoint.Y) < y_moveUnit)
                            startPoint.Y = endPoint.Y;
                        else
                        {
                            startPoint.X -= x_moveUnit;
                            startPoint.Y -= y_moveUnit;
                        }
                        DrawCircle(startPoint, Sim_Nov_13.Properties.Resources.mouseNorthWest);


                        break;
                }
                Thread.Sleep(speed);

                #endregion

                ismoved = 1;
            }
            Thread.Sleep(1000);// to stay for one sec on selected place          
            return ismoved;
            

        }

推荐答案

很难说是怎么回事,但我是猜测你有一个跨线程调用问题。除此之外,你真的没有像你想象的那样使用线程。对于初学者,我会考虑使用 BackgroundWorker [ ^ 而不是你这样做的方式。



这是一个问题:



Its hard to say what is going on, but I'm guessing you have a cross-thread call problem. Other than that, you really aren't using threading like you are supposed to. For a beginner, I would look at using BackgroundWorker[^] instead of the way you are doing it.

Here is one problem:

moveCircleThread.Start();
moveCircleThread.Join();





你启动一个新线程,然后立即调用加入 [ ^ ],这会导致当前(UI)线程加入您启动并等待的线程(Block )直到它完成。如果您刚刚调用了没有线程的moveCircle,那就相同了。



You start a new thread, then immediately call Join[^], which causes the current (UI) thread to join the thread you started and wait (Block) there until its finished. It would be the same if you had just called moveCircle without the threading.


这篇关于线程自动停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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