Ejemplo n.º 1
0
        private void CodeThreadsFunc(object param)
        {
            CodeThreadInfo threadParamObj = (CodeThreadInfo)param;
            string         realCode;

            if (GetCode(threadParamObj.CodeImg, out realCode))
            {
                threadParamObj.RealCode = realCode;
                threadParamObj.WaitEvent.Set();
            }
        }
Ejemplo n.º 2
0
        //private Dictionary<string, List<Thread>> _codeThreadMap = new Dictionary<string, List<Thread>>();
        public bool GetCodeByMutilThread(Image codeImg, int threadCount, out string realCode, out string cost)
        {
            //Guid guid = Guid.NewGuid();
            //string taskKey = guid.ToString();
            Stopwatch sw = new Stopwatch();

            sw.Start();
            List <Thread>  threadList  = new List <Thread>();
            AutoResetEvent waitEvent   = new AutoResetEvent(false);
            CodeThreadInfo threadParam = new CodeThreadInfo();

            threadParam.CodeImg   = codeImg;
            threadParam.WaitEvent = waitEvent;
            for (int i = 0; i < threadCount; i++)
            {
                Thread codeThread = new Thread(new ParameterizedThreadStart(CodeThreadsFunc));
                threadList.Add(codeThread);
            }

            foreach (Thread codeThread in threadList)
            {
                codeThread.Start(threadParam);
            }
            if (waitEvent.WaitOne(1000 * 5))
            {
                realCode = threadParam.RealCode;
                sw.Stop();
                cost = (sw.ElapsedMilliseconds / (float)1000).ToString("F2");
                return(true);
            }
            else
            {
                realCode = "Timeout";
                sw.Stop();
                cost = (sw.ElapsedMilliseconds / (float)1000).ToString("F2");
                return(false);
            }
        }