Search code examples
c#.netmanualresetevent

How to use ManualResetEvent without freezing my UI


I want to use ManualResetEvent instead of Thread.Sleep to prevent my UI from freeing.

This is what I have tried:

private ManualResetEvent manualResetEvent = null;

private void Form1_Load(object sender, EventArgs e)
{
    ManualResetEvent manualResetEvent = new ManualResetEvent(false);
}

And after my operation I want to wait 5 seconds:

manualResetEvent.WaitOne(5000);

My problem is that my UI is still freezing ...


Solution

  • Now the situation is more clear.

    In this case you may use backgroundworker in example define a backgroundworker in your form.

    when timer event id fired you may use

    'assume that backgroundworker is named "BBC"
    'support cancellation task
     BBC.WorkerSupportsCancellation = True
    'enable report progress 
     BBC.WorkerReportsProgress = True
    'run it        
     BBC.RunWorkerAsync()
    

    When it finish in BBC_RunWorkerCompleted or your delegate to the method you can show a message in trhead safe mode.

    that's all your from wont freeze, you will be able to use as well your application and so on.I use this method for long tasks or multiple tasks like deletion and so on.You have to set your own method/logic to see if your file has been created into the folder or not.