البرمجة

تنفيذ تشغيل ملفات دفعية بشكل متزامن

The issue you’re facing with TParallel.For might be related to the fact that TParallel.For uses a pool of threads to execute the iterations in parallel. This means that the iterations are not guaranteed to execute in a specific order or with a specific timing.

One possible reason for the discrepancy in the number of out.txt files generated could be that some iterations are taking longer to complete than others, and the pool of threads might be reusing threads that have already finished their previous iterations. This can lead to some iterations not being completed by the time TParallel.For finishes its execution.

To ensure that all iterations are completed before continuing, you can use a synchronization mechanism such as a TEvent or TMonitor. Here’s an example of how you can modify your code to wait for all iterations to complete before continuing:

delphi
var Event: TEvent; begin Event := TEvent.Create(nil, True, False, ''); try TParallel.For(1, 100, procedure(i: Integer) begin SetCurrentDir(Path + '\test\' + IntToStr(i)); WinExec32AndWait(PChar('run.bat'), 0); Event.SetEvent; end); // Wait for all iterations to complete Event.WaitFor(TEVENT.INFINITE); finally Event.Free; end; end;

This modification ensures that the main thread waits for all iterations to complete before continuing, which should help ensure that all out.txt files are generated.

المزيد من المعلومات

بالتأكيد! المشكلة التي تواجهها قد تكون ناتجة عن استخدام TParallel.For بشكل غير صحيح أو عدم تزامن عمليات الإنهاء. عند استخدام TParallel.For ، يتم تقسيم العمليات بين عدة مواضيع لتنفيذها بشكل متزامن، ولكنها قد لا تنتهي بنفس الترتيب الذي بدأت به.

لضمان اكتمال جميع العمليات قبل المتابعة، يمكنك استخدام TTask و WaitForAll لمراقبة وانتظار اكتمال كل مهمة على حدة. هناك كود مثال يوضح ذلك:

delphi
uses System.Threading, System.SysUtils; procedure RunBatchFile(const Path: string; Index: Integer); begin TThread.Synchronize(nil, procedure begin SetCurrentDir(Path + '\test\' + IntToStr(Index)); end); WinExec32AndWait(PChar('run.bat'), 0); end; var Tasks: TArray; I: Integer; begin SetLength(Tasks, 100); for I := Low(Tasks) to High(Tasks) do begin Tasks[I] := TTask.Run( procedure begin RunBatchFile('your_path_here', I + 1); end); end; TTask.WaitForAll(Tasks); end;

يرجى استبدال 'your_path_here' بالمسار الصحيح لمجلدات الاختبار الخاصة بك. هذا الكود يقوم بتشغيل كل دفعة في مهمة فرعية وينتظر اكتمال كل المهام قبل المتابعة.

مقالات ذات صلة

زر الذهاب إلى الأعلى
إغلاق

أنت تستخدم إضافة Adblock

يرجى تعطيل مانع الإعلانات حيث أن موقعنا غير مزعج ولا بأس من عرض الأعلانات لك فهي تعتبر كمصدر دخل لنا و دعم مقدم منك لنا لنستمر في تقديم المحتوى المناسب و المفيد لك فلا تبخل بدعمنا عزيزي الزائر