2021-11-06T10:52:10+01:00
2021-11-06T11:07:43+01:00
2022-08-12T05:30:29+02:00
helloc
Sziasztok!
Szeretnék beépíteni egy gombot (button5), aminek a megnyomása esetén lehetne szüneteltetni, illetve aztán tovább folytatni a FileSystemWatcher működését. Hogyan tudnám ezt beépíteni az alábbi kódba?
Form1.cs
Szeretnék beépíteni egy gombot (button5), aminek a megnyomása esetén lehetne szüneteltetni, illetve aztán tovább folytatni a FileSystemWatcher működését. Hogyan tudnám ezt beépíteni az alábbi kódba?
Form1.cs
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
namespace FirstApp
{
public partial class Form1 : Form
{
public static string path_import = "";
public static string path_export = "";
FileSystemWatcher watcher;
SemaphoreSlim semaphore;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
numericUpDown1.Enabled = false;
int i = (int)numericUpDown1.Value;
semaphore = new SemaphoreSlim(i, i);
this.watch();
}
private void watch()
{
watcher = new FileSystemWatcher();
watcher.Path = Form1.path_import;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.mp4";
watcher.Renamed += OnRenamed;
watcher.EnableRaisingEvents = true;
}
private void OnRenamed(object source, RenamedEventArgs e)
{
Task.Run(async() => {
await Task.Delay(1000);
await semaphore.WaitAsync();
try
{
ProcessRenaming(e.FullPath, e.Name);
}
finally
{
semaphore.Release();
}
});
}
private void ProcessRenaming(string fullPath, string name)
{
var lastPart = fullPath.Split('.').Last();
if (lastPart == "mp4")
{
File.Move(fullPath, fullPath + ".firstapp");
string nf = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(name);
Process prcExecuteFFMPEG = new Process();
prcExecuteFFMPEG.StartInfo.FileName = "ffmpeg.exe";
prcExecuteFFMPEG.StartInfo.Arguments = "-ss 00:00:30 -i "" + fullPath + ".firstapp" -c copy "" + path_export + "" + nf + """;
prcExecuteFFMPEG.StartInfo.UseShellExecute = false;
prcExecuteFFMPEG.StartInfo.RedirectStandardOutput = true;
prcExecuteFFMPEG.StartInfo.CreateNoWindow = true;
prcExecuteFFMPEG.Start();
prcExecuteFFMPEG.WaitForExit();
string strOutput = prcExecuteFFMPEG.StandardOutput.ReadToEnd();
File.Delete(fullPath + ".firstapp");
}
}
private void button5_Click(object sender, EventArgs e)
{
// ...
}
}
}
Mutasd a teljes hozzászólást!
- Csaboka2válasza helloc (10:52) részére
- 2021.11.06. 11:07
- permalink
A doksi szerint az EnableRaisingEvents tulajdonságot false-ra állítod, és akkor nem fog eseményeket generálni, amíg megint vissza nem rakod true-ra.Mutasd a teljes hozzászólást!