autoresetevent

Dispatcher.Invoke from a new thread is locking my UI

China☆狼群 提交于 2019-12-10 10:08:03
问题 i'm using wpf, there's a button on my ui. when the user clicks it, i have a for loop that runs a new method, on a new thread using autoresetevent. in that method on that new thread, i'm using a label, let's call it lblStatus. i want to update that label on this thread that's not on the ui. using wpf, i have to use Dispatcher.Invoke. here's a sample of my code: Thread thread= new Thread(StartLooking); thread.Start(); _waitHandle.WaitOne(); private void StartLooking(object value) { if

new AutoResetEvent (true) Usages in C#?

假装没事ソ 提交于 2019-12-10 01:00:34
问题 I was wondering , Why would I ever want to pass a true in the ctor of AutoResetEvent ? I create a waitHandle so that anyone who will call WaitOne() will actually wait . If I instance it with a true , it will be as if it was immediatly signaled - which is like a normal flow without waiting. EventWaitHandle _waitHandle = new AutoResetEvent (false); void Main() { new Thread (Waiter).Start(); Thread.Sleep (1000); _waitHandle.Set(); Console.ReadLine(); } void Waiter() { Console.WriteLine ("AAA");

AutoResetEvent Reset immediately after Set

…衆ロ難τιáo~ 提交于 2019-12-09 10:43:59
问题 Consider the following pattern: private AutoResetEvent signal = new AutoResetEvent(false); private void Work() { while (true) { Thread.Sleep(5000); signal.Set(); //has a waiting thread definitely been signaled by now? signal.Reset(); } } public void WaitForNextEvent() { signal.WaitOne(); } The purpose of this pattern is to allow external consumers to wait for a certain event (e.g. - a message arriving). WaitForNextEvent is not called from within the class. To give an example that should be

Dispatcher.Invoke from a new thread is locking my UI

做~自己de王妃 提交于 2019-12-05 21:22:27
i'm using wpf, there's a button on my ui. when the user clicks it, i have a for loop that runs a new method, on a new thread using autoresetevent. in that method on that new thread, i'm using a label, let's call it lblStatus. i want to update that label on this thread that's not on the ui. using wpf, i have to use Dispatcher.Invoke. here's a sample of my code: Thread thread= new Thread(StartLooking); thread.Start(); _waitHandle.WaitOne(); private void StartLooking(object value) { if (lblStatus.Dispatcher.Thread == Thread.CurrentThread) { lblStatus.Content = "Scanning>..."; } else { lblStatus

AutoResetEvent not blocking properly

隐身守侯 提交于 2019-12-05 06:31:12
问题 I have a thread, which creates a variable number of worker threads and distributes tasks between them. This is solved by passing the threads a TaskQueue object, whose implementation you will see below. These worker threads simply iterate over the TaskQueue object they were given, executing each task. private class TaskQueue : IEnumerable<Task> { public int Count { get { lock(this.tasks) { return this.tasks.Count; } } } private readonly Queue<Task> tasks = new Queue<Task>(); private readonly

Using BackgroundWorker to complete two methods one after the other WPF/C#

二次信任 提交于 2019-12-04 04:53:43
问题 In my program I have two methods that takes a while to complete, about few minutes each. While these methods are being executed, I display a Progress Bar in a separate window which shows the progress of each method. My two methods are in a static Utility class. They look like the following: public static class Utility { public static bool TimeConsumingMethodOne(object sender) { for (int i = 1; i <= 100; i++) { Thread.Sleep(100); (sender as BackgroundWorker).ReportProgress(i); } return true; }

AutoResetEvent Reset immediately after Set

久未见 提交于 2019-12-03 13:47:36
Consider the following pattern: private AutoResetEvent signal = new AutoResetEvent(false); private void Work() { while (true) { Thread.Sleep(5000); signal.Set(); //has a waiting thread definitely been signaled by now? signal.Reset(); } } public void WaitForNextEvent() { signal.WaitOne(); } The purpose of this pattern is to allow external consumers to wait for a certain event (e.g. - a message arriving). WaitForNextEvent is not called from within the class. To give an example that should be familiar, consider System.Diagnostics.Process . It exposes an Exited event, but it also exposes a

Java's equivalent to .Net's AutoResetEvent?

…衆ロ難τιáo~ 提交于 2019-12-03 05:55:04
问题 What should I use to get semantics equivalent to AutoResetEvent in Java? (See this question for ManualResetEvent). 回答1: @user249654's answer looked promising. I added some unit tests to verify it, and indeed it works as expected. I also added an overload of waitOne that takes a timeout. The code is here in case anyone else finds it useful: Unit Test import org.junit.Assert; import org.junit.Test; import static java.lang.System.currentTimeMillis; /** * @author Drew Noakes http://drewnoakes.com

Java's equivalent to .Net's AutoResetEvent?

橙三吉。 提交于 2019-12-02 19:19:05
What should I use to get semantics equivalent to AutoResetEvent in Java? (See this question for ManualResetEvent). @user249654's answer looked promising. I added some unit tests to verify it, and indeed it works as expected. I also added an overload of waitOne that takes a timeout. The code is here in case anyone else finds it useful: Unit Test import org.junit.Assert; import org.junit.Test; import static java.lang.System.currentTimeMillis; /** * @author Drew Noakes http://drewnoakes.com */ public class AutoResetEventTest { @Test public void synchronisesProperly() throws InterruptedException {

Using BackgroundWorker to complete two methods one after the other WPF/C#

会有一股神秘感。 提交于 2019-12-02 00:45:55
In my program I have two methods that takes a while to complete, about few minutes each. While these methods are being executed, I display a Progress Bar in a separate window which shows the progress of each method. My two methods are in a static Utility class. They look like the following: public static class Utility { public static bool TimeConsumingMethodOne(object sender) { for (int i = 1; i <= 100; i++) { Thread.Sleep(100); (sender as BackgroundWorker).ReportProgress(i); } return true; } public static bool TimeConsumingMethodTwo(object sender) { for (int i = 1; i <= 100; i++) { Thread