如何在c#中有效地使用Thread.sleep()

时间:2015-12-24 05:08:34

标签: c# multithreading

我使用多线程,当我使用多线程时,cpu使用率是100%结束。

多线程计数是可变的。有时计数范围在200到400之间

所以我需要在我的应用程序中减少cpu使用量。

private void _ThreadWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var DataSpots = e.Argument as Dictionary<PointF, List<LogScanCollection>>;
            if (DataSpots != null)
            {
                var UpLoadThread = new Thread[DataSpots.Count];

                for (int i = 0; i < UpLoadThread.Length; i++)
                {
                    int temp = i;
                    var item = DataSpots.ElementAt(i);
                    var ItemKey = item.Key;
                    var ItemValue = item.Value;
                    UpLoadThread[temp] = new Thread(() => DataUpload(ItemKey, ItemValue, UpLoadThread[temp]));
                    UpLoadThread[temp].IsBackground = true;
                }
                foreach (Thread t in UpLoadThread)
                {
                    t.Start();
                    Thread.Sleep(100);
                }

                foreach (Thread t in UpLoadThread)
                {
                    t.Join();
                }
            }
        }

处理线程方法

private void DataUpload(PointF ItemKey, List<LogScanCollection> ItemValue, Thread thread)
        {
            object db = new object();
            lock (db)
            {
                bool DmsXYExist = DmsXYKeyExist(ItemKey);
                string mtime = ItemValue[0].MeasTime.ToString("yyyy-MM-dd HH:mm:ss");
                PointF GPS = ItemValue[0].GPS;
                if (DmsXYExist)
                {
                    if (ItemValue.Count > 1)
                    {
                        var DataList = GetCalMergeData(ItemValue);
                        var DbData = GetDbData(ItemKey);

                        List<double> min = ChangeDataList(DataList.Min, DbData.Min, DataCalType.Min);
                        List<double> max = ChangeDataList(DataList.Max, DbData.Max, DataCalType.Max);
                        List<double> aver = ChangeDataList(DataList.Aver, DbData.Aver, DataCalType.Aver);
                        List<double> exp = ChangeDataList(DataList.Exp, DbData.Exp, DataCalType.Aver);
                        if (!CheckDupDataFromTime(mtime, "vmmin"))
                        {
                            UpdateDataQuery("vmmin", GPS, ItemKey, min, mtime, "min");
                            UpdateDataQuery("vmmax", GPS, ItemKey, max, mtime, "max");
                            UpdateDataQuery("vmaver", GPS, ItemKey, aver, mtime, "aver");
                            UpdateDataQuery("expidx", GPS, ItemKey, exp, mtime, "exp");
                        }
                    }
                    else
                    {
                        var Fs = GetFieldStrengthArray(ItemValue[0].Data3G);
                        var Ei = GetExpIdxArray(ItemValue[0].Data3G);
                        var DbData = GetDbData(ItemKey);
                        List<double> min = ChangeDataList(Fs, DbData.Min, DataCalType.Min);
                        List<double> max = ChangeDataList(Fs, DbData.Max, DataCalType.Max);
                        List<double> aver = ChangeDataList(Fs, DbData.Aver, DataCalType.Aver);
                        List<double> exp = ChangeDataList(Ei, DbData.Exp, DataCalType.Aver);

                        if (!CheckDupDataFromTime(mtime, "vmmin"))
                        {
                            UpdateDataQuery("vmmin", GPS, ItemKey, min, mtime, "min");
                            UpdateDataQuery("vmmax", GPS, ItemKey, max, mtime, "max");
                            UpdateDataQuery("vmaver", GPS, ItemKey, aver, mtime, "aver");
                            UpdateDataQuery("expidx", GPS, ItemKey, exp, mtime, "exp");
                        }
                    }
                }
                else
                {
                    InsertDmsXYTableKey(ItemKey, mtime);
                    if (ItemValue.Count > 1)
                    {
                        var DataList = GetCalMergeData(ItemValue);
                        InsertDataQuery("vmmin", GPS, ItemKey, DataList.Min, mtime);
                        InsertDataQuery("vmmax", GPS, ItemKey, DataList.Max, mtime);
                        InsertDataQuery("vmaver", GPS, ItemKey, DataList.Aver, mtime);
                        InsertDataQuery("expidx", GPS, ItemKey, DataList.Exp, mtime);
                    }
                    else
                    {
                        InsertDataQuery("vmmin", GPS, ItemKey, GetFieldStrengthArray(ItemValue[0].Data3G), mtime);
                        InsertDataQuery("vmmax", GPS, ItemKey, GetFieldStrengthArray(ItemValue[0].Data3G), mtime);
                        InsertDataQuery("vmaver", GPS, ItemKey, GetFieldStrengthArray(ItemValue[0].Data3G), mtime);
                        InsertDataQuery("expidx", GPS, ItemKey, GetExpIdxArray(ItemValue[0].Data3G), mtime);

                    }
                }
                for (int i = 0; i < ItemValue.Count; i++)
                {
                    string mtime1 = ItemValue[i].MeasTime.ToString("yyyy-MM-dd HH:mm:ss");
                    double rbw = ItemValue[i].Data3G.Frequency[1] - ItemValue[0].Data3G.Frequency[0];
                    int length = ItemValue[i].Data3G.Frequency.Length;
                    if (!CheckDupDataFromTime(mtime1, "tr3g"))
                    {
                        InsertTraceData("tr3g", mtime1, ItemValue[i].GPS, ItemKey, ItemValue[i].Data3G.MergeByteXYZ, rbw, length);
                    }
                    if (!CheckDupDataFromTime(mtime1, "tr6g"))
                    {
                        InsertTraceData("tr6g", mtime1, ItemValue[i].GPS, ItemKey, ItemValue[i].Data6G, rbw, length);
                    }

                }
                _ThreadWorker.ReportProgress(0);

                if(thread.IsAlive)
                {
                    thread.Abort();
                }
            }


        }

请让我知道如何减少CPU使用率并有效使用Thread

谢谢!

0 个答案:

没有答案