|
 |
|
C# WinApp ทำยังไง หรือใช้ library ในการดึงค่า Duration ออกมาอย่างเร็วๆ ได้บ้างครับ |
|
 |
|
|
 |
 |
|
ตอนนี้ผมใช้ Mp3FileReader อยู่ (เพราะ TagLib ลองใช้แล้วเวลาไม่ตรงเท่าไหร่)
Code (C#)
public static TimeSpan GetMp3Duration(this string filePath)
{
TimeSpan duration = new TimeSpan();
try
{
using (var reader = new Mp3FileReader(filePath))
duration = reader.TotalTime;
}
catch
{
// จัดการกับข้อผิดพลาดที่เกิดขึ้นเมื่อไฟล์ MP3 มีปัญหา
// ตัวอย่าง: ไฟล์ไม่สมบูรณ์, ไฟล์ไม่มีข้อมูล Duration
duration = TimeSpan.Zero; // หรือค่าเริ่มต้นที่คุณต้องการ
}
return duration;
}
public static string GetMp3DurationToString(this string filePath)
{
TimeSpan duration = GetMp3Duration(filePath);
if (duration.TotalHours >= 1)
{
// ถ้ามีหน่วยชั่วโมงให้แสดงหน่วยชั่วโมงด้วย
return string.Format("{0:%h}:{0:%m}:{0:%s}", duration);
}
else if (duration.TotalMinutes >= 1)
{
// ถ้าไม่มีหน่วยชั่วโมง ให้แสดงเฉพาะนาทีและวินาที
return string.Format("{0:%m}:{0:%s}", duration);
}
else {
// ถ้าไม่มีหน่วยชั่วโมง นาที ให้แสดงเฉพาะวินาที
return string.Format("{0:%s}", duration);
}
}
Code (C#)
public static async Task RunLimitedWithSemaphoreSlim(IEnumerable<Func<Task>> actions, int maxConcurrency)
{
await Task.Run(async() =>
{
var semaphore = new SemaphoreSlim(maxConcurrency);
var tasks = actions.Select(action => Task.Run(async () =>
{
await semaphore.WaitAsync();
try
{
await action(); // Execute the action
}
finally
{
semaphore.Release();
}
})).ToArray();
await Task.WhenAll(tasks);
});
}
Code (C#)
void GetMp3() {
Task.Run(async () =>
{
int countTask = 30;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var Files = Directory.GetFiles("D:\\Song\\SongTOR\\SongTOR", "*.mp3");
this.progressBar1.SetMinMax(0, Files.Length);
List<Func<Task>> actions = new List<Func<Task>>();
foreach (var f in Files)
{
string file = f;
actions.Add(async () => await Task.Run(() =>
{
this.Write($"Time:{stopwatch.Elapsed}");
richTextBox1.WriteLine($"File:{f} Size:{FileTor.GetFileSizeToString(f) } Duration:{extFileTor.GetMp3DurationToString(f)}");
this.progressBar1.AddValue();
}));
}
await LimitedTaskSchedulerHelper.RunLimitedWithSemaphoreSlim(actions, countTask);
stopwatch.Stop();
TimeSpan tagLibTime = stopwatch.Elapsed;
richTextBox1.WriteLine($"Files:{Files.Length}\nMp3FileReader Time: {tagLibTime}");
this.Write("Complete");
});
}
ผ่านไปเป็นนาที ยังรันไปได้ไม่กี่ไฟล์ เองครับ


ช้ามากกกกกกกก
Tag : .NET, Win (Windows App), C#

|
ประวัติการแก้ไข 2024-06-27 23:08:48
|
 |
 |
 |
 |
Date :
2024-06-27 23:04:03 |
By :
lamaka.tor |
View :
205 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าอ่านจากไฟล์ มันจะช้า ต้อง ผ่านการ อ่าน->convert ถีงจะได้ข้อมูล
ไม่เก็บข้อมูลเลื้องต้น ไว้ในตาราง database ไว้ก่อนล่ะ
เวลาใช้งานก็อ่านจากตารางมาใช้ เวลา แสดงไฟล์ค่อยอ่านไฟล์มาใช้งาน
|
 |
 |
 |
 |
Date :
2024-06-28 18:50:26 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|