简体   繁体   中英

Freeze issue when using Parallel.ForEach

I'm using Parallel.ForEach as the sample:

ParallelOptions parallelOpt = new ParallelOptions();
parallelOpt.MaxDegreeOfParallelism = 2;
Parallel.ForEach(list, parallelOpt, info =>
    {
        int threadID = Thread.CurrentThread.ManagedThreadId;
        //use tmp var to avoid conflic with original var when using thread
        ML_Scheduler tmp = new ML_Scheduler();
        tmp = ObjectCopier.Clone<ML_Scheduler>(info);
        Stopwatch sw1 = Stopwatch.StartNew();
        logger.Info("\r\n\r\n");
        logger.Info(string.Format("[{0}]****** Begin to schedule work with ThreadID {0} for ScheduleID {1}.", threadID, tmp.SchedulerID));
        WorkWithEachSchedule(threadID, tmp, tmp.SchedulerID, dtNow, tmp.StartTime, false);
        TimeSpan time1 = TimeSpan.FromSeconds(sw1.ElapsedMilliseconds / 1000.0);
        logger.Info(string.Format("[{0}]****** Finish to work with ThreadID {0} (during time : {1}).", threadID, time1.ToString(@"hh\:mm\:ss\:fff")));
    }
);

In function WorkWithEachSchedule, I just do:

  1. Create datatable with around 10000 records for testing.

  2. Export datatable to excel 2007

  3. Save to local folder.

In the beginning, when I check Task Manager in Details, I see that the CPU and Memory is working.

But after some minutes, the program will freeze (as the sample image) 在此处输入图片说明

Both of CPU and Memory do not work, the program does not work anything more.

And I also cannot debug by breakpoint.

Update:

In my code, I use function ExporttoExcel as following link: http://mikesknowledgebase.azurewebsites.net/pages/CSharp/ExportToExcel.htm

And when I remove this function, the program can works correct.

I don't know why that function causes the issue.

Any advice. Thanks.

Please note, that the author of the library you are using noted this:

There is also an issue when writing lots of rows (30,000+) to the Excel file. The OpenXML libraries simply seem to hang when there's too much data to write. I haven't implemented a solution to this problem yet.

The author noted, that he solved the issue, but hanging could mean, that it is still issue or perhaps it was not meant to be used concurrently...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM