Hi guys...
i created a form names progressForm.
i add in this a label and a progress bar
this is the code
using System;
using System.Windows.Forms;
namespace MiniTools
{
public partial class ProgressForm : Form
{
private readonly Timer updateTimer;
private string progressMessage;
private bool isUpdating;
private int taskCount;
private int currentTask;
public ProgressForm()
{
InitializeComponent();
// Initialize Timer
updateTimer = new Timer();
updateTimer.Interval = 1000; // 1 second
updateTimer.Tick += UpdateTimer_Tick;
updateTimer.Start();
// Set initial message
progressMessage = "Please wait...";
isUpdating = false;
taskCount = 0;
currentTask = 0;
// Set form properties to make it modal and prevent user interaction
this.FormBorderStyle = FormBorderStyle.FixedDialog; // Make the window fixed size
this.ControlBox = false; // Disable close button
this.StartPosition = FormStartPosition.CenterScreen; // Center the form
}
private void UpdateTimer_Tick(object sender, EventArgs e)
{
if (!isUpdating)
{
// Show "Please Wait..." message
statusLabel.Text = progressMessage;
}
}
public void UpdateProgress(string message, int taskIndex, int totalTasks)
{
progressMessage = message;
isUpdating = true;
statusLabel.Text = message;
// Update Progress Bar
taskCount = totalTasks > 100 ? 100 : totalTasks; // If tasks are more than 100, set taskCount to 100
currentTask = taskIndex;
int progressValue = totalTasks > 100 ? (int)((double)taskIndex / totalTasks * 100) : taskIndex;
progressBar.Value = Math.Min(progressValue, 100); // Ensure the value does not exceed 100
// Reset the timer
updateTimer.Stop();
updateTimer.Start();
isUpdating = false;
// Close form automatically when all tasks are complete
if (taskIndex >= totalTasks)
{
Close();
}
}
}
}
and this is how i handel it
public async void SaveSnapshots(Vegas vegas)
{
using (ProgressForm progressForm = new ProgressForm())
{
progressForm.Show(); // Show the progress form in non-modal mode
try
{
// Save original project and project settings
VideoRenderQuality originalPreviewRenderQuality = vegas.Project.Preview.RenderQuality;
bool originalPreviewFillSize = vegas.Project.Preview.FullSize;
// Set preview quality and size
vegas.Project.Preview.RenderQuality = VideoRenderQuality.Best;
vegas.Project.Preview.FullSize = true;
vegas.UpdateUI();
// Create 'Snapshots' folder in the project directory if it doesn't exist
string snapshotFolder = Path.Combine(Path.GetDirectoryName(vegas.Project.FilePath), "Snapshots");
if (!Directory.Exists(snapshotFolder))
{
Directory.CreateDirectory(snapshotFolder);
}
// Get the existing snapshot files
string[] existingSnapshots = Directory.GetFiles(snapshotFolder, "Snapshot-*.jpg");
// Determine the starting counter based on existing snapshots
int snapshotCounter = existingSnapshots.Length + 1;
// Update progress form
progressForm.UpdateProgress("Saving snapshots...", 0, vegas.Project.Markers.Count);
// Run the snapshot saving process in a background thread
await Task.Run(() =>
{
int currentMarkerIndex = 0;
foreach (Marker marker in vegas.Project.Markers)
{
try
{
// Set the cursor time to the marker position
vegas.Transport.CursorPosition = marker.Position;
// Create snapshot file name
string snapshotFilePath = Path.Combine(snapshotFolder, $"Snapshot-{snapshotCounter}.jpg");
// Increment the counter for the next snapshot
snapshotCounter++;
// Save the snapshot
vegas.SaveSnapshot(snapshotFilePath, ImageFileFormat.JPEG, vegas.Transport.CursorPosition);
// Update progress on the UI thread
progressForm.Invoke((Action)(() =>
{
currentMarkerIndex++;
progressForm.UpdateProgress($"Saving snapshot {currentMarkerIndex}...", currentMarkerIndex, vegas.Project.Markers.Count);
}));
}
catch (Exception ex)
{
MessageBox.Show($"Error saving snapshot at marker: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
});
// Restore project preview quality setting
vegas.Project.Preview.RenderQuality = originalPreviewRenderQuality;
vegas.Project.Preview.FullSize = originalPreviewFillSize;
vegas.UpdateUI();
progressForm.UpdateProgress("Snapshots saved successfully!", vegas.Project.Markers.Count, vegas.Project.Markers.Count);
}
catch (Exception ex)
{
// Display an error message if something goes wrong
MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// Ensure the progress form is closed
progressForm.Close();
}
}
}
the problem is i want when this form launches it should freeze everything to reach in VEGAS until form closed automatically.
but is blocking UI thread when i use ShowDialog() and it freeze VEGAS and but when i user show() then it working but if i move the progressForm dialog while its processing. it again got freezed and crashed.
i am so confused.
can anybody guide me in this...