I am using Unity 3D and I want to run task in main thread and await it to complete.
Here is my code:
public override Task<Vibranium.Protobuffers.ACSpawnData> WorldObjectSpawned(Vibranium.Protobuffers.WorldObjectData request, ServerCallContext context)
{
ACSpawnData acSpawnData = new ACSpawnData();
IEnumerator SpawnGo(ACSpawnData acSpawnData)
{
int spawnId = WorldObjectManager.Instance.GetWorldObjects.Count + 1;
GameObject spawnedGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
spawnedGO.name = spawnId.ToString();
spawnedGO.transform.SetParent(WorldObjectManager.Instance.transform);
if (request.WorldObjectType == WorldObjectType.Player)
{
spawnedGO.GetComponent<Renderer>().material.color = Color.red;
}
WorldObjectManager.Instance.GetWorldObjects.Add(spawnedGO);
acSpawnData.SpawnId = (uint)spawnId;
acSpawnData.WorldObjectType = request.WorldObjectType;
yield return null;
}
MainThreadDispatcher.Instance().Enqueue(SpawnGo(acSpawnData));
return Task.FromResult(acSpawnData);
}
To execute IEnumerator in main thread I use this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
/// Author: Pim de Witte (pimdewitte.com) and contributors, https://github.com/PimDeWitte/UnityMainThreadDispatcher
/// <summary>
/// A thread-safe class which holds a queue with actions to execute on the next Update() method. It can be used to make calls to the main thread for
/// things such as UI Manipulation in Unity. It was developed for use in combination with the Firebase Unity plugin, which uses separate threads for event handling
/// </summary>
public class MainThreadDispatcher : MonoBehaviour {
private static readonly Queue<Action> _executionQueue = new Queue<Action>();
public void Update() {
lock(_executionQueue) {
while (_executionQueue.Count > 0) {
_executionQueue.Dequeue().Invoke();
}
}
}
/// <summary>
/// Locks the queue and adds the IEnumerator to the queue
/// </summary>
/// <param name="action">IEnumerator function that will be executed from the main thread.</param>
public void Enqueue(IEnumerator action) {
lock (_executionQueue) {
_executionQueue.Enqueue (() => {
StartCoroutine (action);
});
}
}
/// <summary>
/// Locks the queue and adds the Action to the queue
/// </summary>
/// <param name="action">function that will be executed from the main thread.</param>
public void Enqueue(Action action)
{
Enqueue(ActionWrapper(action));
}
/// <summary>
/// Locks the queue and adds the Action to the queue, returning a Task which is completed when the action completes
/// </summary>
/// <param name="action">function that will be executed from the main thread.</param>
/// <returns>A Task that can be awaited until the action completes</returns>
public Task EnqueueAsync(Action action)
{
var tcs = new TaskCompletionSource<bool>();
void WrappedAction() {
try
{
action();
tcs.TrySetResult(true);
} catch (Exception ex)
{
tcs.TrySetException(ex);
}
}
Enqueue(ActionWrapper(WrappedAction));
return tcs.Task;
}
IEnumerator ActionWrapper(Action a)
{
a();
yield return null;
}
private static MainThreadDispatcher _instance = null;
public static bool Exists() {
return _instance != null;
}
public static MainThreadDispatcher Instance() {
if (!Exists ()) {
throw new Exception ("UnityMainThreadDispatcher could not find the UnityMainThreadDispatcher object. Please ensure you have added the MainThreadExecutor Prefab to your scene.");
}
return _instance;
}
void Awake() {
if (_instance == null) {
_instance = this;
DontDestroyOnLoad(this.gameObject);
}
}
void OnDestroy() {
_instance = null;
}
}
Everything works fine. The gameobject is created however Task.FromResult does not await SpawnGo to populate acSpawnData so acSpawnData is always returned empty.
How can I block the execution of Task WorldObjectSpawned until IEnumerator SpawnGo does it's job and populate acSpawnData so I can return it?
Instead of doing this directly with your own threading, you should utilize C#'s built-in async/await.
It allows you to spin off tasks, which manage the threads for you, and then you can decide when you need to wait for a task to complete before proceeding.
As a side benefit, it will also significantly streamline and reduce your code.
Related
I have a method that doing Chromium browser initialization. After running two asynchronous method, the program automatically close. Did I use the Task.WhenAll wrong?
Here is the entry point of the program:
Start.cs
static class Start
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Initializer.Start();
}
}
Initializer.cs
public static class Initializer
{
public static async void Start()
{
// The task that automatically close the program.
Task<bool> chromiumInitTask = ChromiumInfo.InitializeAsync();
await chromiumInitTask;
Task webviewInitTask = WebViewInfo.InitializeAsync();
Task guiInitTask = GUIInfo.InitializeAsync();
HardwareManager.Initialize();
await webviewInitTask;
await guiInitTask;
GUIInfo.Layout.ChangeMainDisplay(ChromiumInfo.Browser);
Application.Run(GUIInfo.Layout.GetLayoutForm());
}
}
ChromiumInfo.cs
public static class ChromiumInfo
{
private static CefSettings _settings;
private static ChromiumWebBrowser _browser;
private static BrowserSettings _browserSettings;
private static Dictionary<string, bool> _initTasks = new Dictionary<string, bool>()
{
{ "Settings", false },
{ "Browser Settings", false },
{ "Browser", false },
{ "Cef", false }
};
private static KeyboardHandler _keyboardHandler;
/// <summary>
/// An setting which using on initialize.
/// </summary>
public static CefSettings Setting => _settings;
/// <summary>
/// Representing a browser which can be show in UI.
/// </summary>
public static ChromiumWebBrowser Browser => _browser;
/// <summary>
/// A keyboard handler which handle various keyboard events.
/// </summary>
public static KeyboardHandler KeyboardHandler => _keyboardHandler;
/// <summary>
/// Occur when request to change the browser.
/// </summary>
public static event RequestChangeBrowserEventHandler RequestChangeBrowser;
/// <summary>
/// Initialize all Chromium components in asynchronously.
/// </summary>
/// <param name="initAllScripts">Indicate should initialize all scripts contain in the root script directory.</param>
/// <returns>Return a task can be awaited. True means sucess. Otherwise, return false.</returns>
public static async Task<bool> InitializeAsync(bool initAllScripts = true)
{
Task settingInit = SettingsInitializeAsync();
Task browserSettingInit = BrowserSettingsInitializeAsync();
// The below line that automatically close the program.
await Task.WhenAll(settingInit, browserSettingInit);
Task cefInit = Cef.InitializeAsync(_settings);
await cefInit;
_initTasks["Cef"] = true;
Task browserInit = BrowserInitializeAsync();
await browserInit;
Task eventInit = EventInitializeAsync();
await eventInit;
Task scriptInit = ScriptInitializeAsync();
await scriptInit;
return _initTasks.Values.Where(it => it).Count() == _initTasks.Count;
}
private static async Task SettingsInitializeAsync()
{
try
{
_settings = new CefSettings();
_settings.CommandLineArgsDisabled = false;
_settings.CefCommandLineArgs.Clear();
_settings.CefCommandLineArgs.Add("enable-3d-apis", "1");
_settings.CefCommandLineArgs.Add("enable-webgl-draft-extensions", "1");
_settings.CefCommandLineArgs.Add("enable-gpu", "1");
_settings.CefCommandLineArgs.Add("enable-webgl", "1");
_settings.CefCommandLineArgs.Add("gpu_compositing", "1");
_settings.CefCommandLineArgs.Add("ignore-gpu-blocklist", "1");
await Task.Delay(1000).ConfigureAwait(false);
_initTasks["Settings"] = true;
}
catch (Exception e)
{
SystemLog.Write(e);
}
}
private static async Task BrowserSettingsInitializeAsync()
{
try
{
_browserSettings = new BrowserSettings();
_browserSettings.WebGl = CefState.Enabled;
await Task.Delay(1000).ConfigureAwait(false);
_initTasks["Browser Settings"] = true;
}
catch (Exception e)
{
SystemLog.Write(e);
}
}
private static async Task BrowserInitializeAsync()
{
try
{
_browser = new ChromiumWebBrowser(Properties.Settings.Default.DefaultURL);
_browser.BrowserSettings = _browserSettings;
_browser.Dock = System.Windows.Forms.DockStyle.Fill;
await Task.Delay(1000).ConfigureAwait(false);
_initTasks["Browser"] = true;
}
catch (Exception e)
{
SystemLog.Write(e);
}
}
private static async Task EventInitializeAsync()
{
KeyboardHandler keyboardHandler = new KeyboardHandler();
WebCommandHandler commandHandler = new WebCommandHandler();
_browser.ConsoleMessage += keyboardHandler.Handle;
_browser.ConsoleMessage += commandHandler.Handle;
_browser.AddressChanged += Custom_AddressChanged;
_keyboardHandler = keyboardHandler;
await Task.Delay(1000).ConfigureAwait(false);
}
private static async Task ScriptInitializeAsync()
{
string scriptPath = $#"{ProgramInfo.RootPath}\scripts";
if (Directory.Exists(scriptPath))
{
var files = Directory.GetFiles(scriptPath, "*.js");
files?.ToList().ForEach(f => _browser.GetMainFrame().ExecuteJavaScriptAsync(f, _browser.Address));
}
await Task.Delay(1000).ConfigureAwait(false);
}
private static void Custom_AddressChanged(object sender, AddressChangedEventArgs e)
{
var wv2SiteCount = Properties.Settings.Default.WebToWV2.Cast<string>()
.Where(s => s.IndexOf(e.Address) >= 0).Count();
if (wv2SiteCount > 0)
{
WebViewInfo.Navigate(e.Address);
RequestChangeBrowser?.Invoke(null, new RequestChangeBrowserEventArgs(WebViewInfo.Browser));
}
}
}
This is because of how await works. When await acts on a Task that is incomplete, it returns. So your program is working like this:
Main runs, and calls Initializer.Start()
Initializer.Start() runs and calls ChromiumInfo.InitializeAsync()
ChromiumInfo.InitializeAsync() runs until it calls await Task.WhenAll(settingInit, browserSettingInit)
Because Task.WhenAll returns an incomplete Task, ChromiumInfo.InitializeAsync() returns its own incomplete Task and execution returns to Initializer.Start().
The await in Initializer.Start() sees the incomplete Task and returns its own incomplete Task and execution returns to Main().
Because Main() doesn't act on the Task returned by Initializer.Start(), execution continues to the next line, which is the end of the program.
The solution is fairly simple: Change your Main method to async and use await.
public static async Task Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
await Initializer.Start();
}
The ability to make Main() async is a feature introduced in C# 7.1.
I have a C# project working with input audio Stream from Kinect 1, Kinect 2, Microphone or anything else.
waveIn.DataAvailable += (object sender, WaveInEventArgs e) => {
lock(buffer){
var pos = buffer.Position;
buffer.Write(e.Buffer, 0, e.BytesRecorded);
buffer.Position = pos;
}
};
The buffer variable is a Stream from component A that will be processed by a SpeechRecognition component B working on Streams.
I will add new components C, D, E, working on Streams to compute pitch, detect sound, do finger printing, or anything else ...
How can I duplicate that Stream for components C, D, E ?
Component A send an Event "I have a Stream do what you want" I don't want to reverse the logic by an Event "Give me your streams"
I'm looking for a "MultiStream" that could give me a Stream instance and will handle the job
Component A
var MultiStream buffer = new MultiStream()
...
SendMyEventWith(buffer)
Component B, C, D, E
public void HandleMyEvent(MultiStream buffer){
var stream = buffer.GetNewStream();
var engine = new EngineComponentB()
engine.SetStream(stream);
}
The MultiStream must be a Stream to wrap Write() method (because Stream do not have data available mechanics) ?
If a Stream is Dispose() by Component B the MultiStream should remove it from it's array ?
The MultiStream must throw an exception on Read() to require use of GetNewStream()
EDIT: Kinect 1 provide a Stream itself ... :-( should I use a Thread to pumpit into the MultiStream ?
Did anybody have that kind of MultiStream Class ?
Thanks
I'm not sure if this is the best way to do it or that it's better than the previous answer, and I'm not guaranteeing that this code is perfect, but I coded something that is literally what you asked for because it was fun - a MultiStream class.
You can find the code for the class here: http://pastie.org/10289142
Usage Example:
MultiStream ms = new MultiStream();
Stream copy1 = ms.CloneStream();
ms.Read( ... );
Stream copy2 = ms.CloneStream();
ms.Read( ... );
copy1 and copy2 will contain identical data after the example is ran, and they will continue to get updated as the MultiStream is written to. You can read, update position, and dispose of the cloned streams individually. If disposed the cloned streams will get removed from MultiStream, and disposing of Multistream will close all related and cloned streams (you can change this if it's not the behavior you want). Trying to write to the cloned streams will throw a not supported exception.
Somehow I don't think streams really fit what you're trying to do. You're setting up a situation where a long run of the program is going to continually expand the data requirements for no apparent reason.
I'd suggest a pub/sub model that publishes the received audio data to subscribers, preferably using a multi-threaded approach to minimize the impact of a bad subscriber. Some ideas can be found here.
I've done this before with a processor class that implements IObserver<byte[]> and uses a Queue<byte[]> to store the sample blocks until the process thread is ready for them. Here's are the base classes:
public abstract class BufferedObserver<T> : IObserver<T>, IDisposable
{
private object _lck = new object();
private IDisposable _subscription = null;
public bool Subscribed { get { return _subscription != null; } }
private bool _completed = false;
public bool Completed { get { return _completed; } }
protected readonly Queue<T> _queue = new Queue<T>();
protected bool DataAvailable { get { lock(_lck) { return _queue.Any(); } } }
protected int AvailableCount { get { lock (_lck) { return _queue.Count; } } }
protected BufferedObserver()
{
}
protected BufferedObserver(IObservable<T> observable)
{
SubscribeTo(observable);
}
public virtual void Dispose()
{
if (_subscription != null)
{
_subscription.Dispose();
_subscription = null;
}
}
public void SubscribeTo(IObservable<T> observable)
{
if (_subscription != null)
_subscription.Dispose();
_subscription = observable.Subscribe(this);
_completed = false;
}
public virtual void OnCompleted()
{
_completed = true;
}
public virtual void OnError(Exception error)
{ }
public virtual void OnNext(T value)
{
lock (_lck)
_queue.Enqueue(value);
}
protected bool GetNext(ref T buffer)
{
lock (_lck)
{
if (!_queue.Any())
return false;
buffer = _queue.Dequeue();
return true;
}
}
protected T NextOrDefault()
{
T buffer = default(T);
GetNext(ref buffer);
return buffer;
}
}
public abstract class Processor<T> : BufferedObserver<T>
{
private object _lck = new object();
private Thread _thread = null;
private object _cancel_lck = new object();
private bool _cancel_requested = false;
private bool CancelRequested
{
get { lock(_cancel_lck) return _cancel_requested; }
set { lock(_cancel_lck) _cancel_requested = value; }
}
public bool Running { get { return _thread == null ? false : _thread.IsAlive; } }
public bool Finished { get { return _thread == null ? false : !_thread.IsAlive; } }
protected Processor(IObservable<T> observable)
: base(observable)
{ }
public override void Dispose()
{
if (_thread != null && _thread.IsAlive)
{
//CancelRequested = true;
_thread.Join(5000);
}
base.Dispose();
}
public bool Start()
{
if (_thread != null)
return false;
_thread = new Thread(threadfunc);
_thread.Start();
return true;
}
private void threadfunc()
{
while (!CancelRequested && (!Completed || _queue.Any()))
{
if (DataAvailable)
{
T data = NextOrDefault();
if (data != null && !data.Equals(default(T)))
ProcessData(data);
}
else
Thread.Sleep(10);
}
}
// implement this in a sub-class to process the blocks
protected abstract void ProcessData(T data);
}
This way you're only keeping the data as long as you need it, and you can attach as many process threads as you need to the same observable data source.
And for the sake of completeness, here's a generic class that implements IObservable<T> so you can see how it all fits together. This one even has comments:
/// <summary>Generic IObservable implementation</summary>
/// <typeparam name="T">Type of messages being observed</typeparam>
public class Observable<T> : IObservable<T>
{
/// <summary>Subscription class to manage unsubscription of observers.</summary>
private class Subscription : IDisposable
{
/// <summary>Observer list that this subscription relates to</summary>
public readonly ConcurrentBag<IObserver<T>> _observers;
/// <summary>Observer to manage</summary>
public readonly IObserver<T> _observer;
/// <summary>Initialize subscription</summary>
/// <param name="observers">List of subscribed observers to unsubscribe from</param>
/// <param name="observer">Observer to manage</param>
public Subscription(ConcurrentBag<IObserver<T>> observers, IObserver<T> observer)
{
_observers = observers;
_observer = observer;
}
/// <summary>On disposal remove the subscriber from the subscription list</summary>
public void Dispose()
{
IObserver<T> observer;
if (_observers != null && _observers.Contains(_observer))
_observers.TryTake(out observer);
}
}
// list of subscribed observers
private readonly ConcurrentBag<IObserver<T>> _observers = new ConcurrentBag<IObserver<T>>();
/// <summary>Subscribe an observer to this observable</summary>
/// <param name="observer">Observer instance to subscribe</param>
/// <returns>A subscription object that unsubscribes on destruction</returns>
/// <remarks>Always returns a subscription. Ensure that previous subscriptions are disposed
/// before re-subscribing.</remarks>
public IDisposable Subscribe(IObserver<T> observer)
{
// only add observer if it doesn't already exist:
if (!_observers.Contains(observer))
_observers.Add(observer);
// ...but always return a new subscription.
return new Subscription(_observers, observer);
}
// delegate type for threaded invocation of IObserver.OnNext method
private delegate void delNext(T value);
/// <summary>Send <paramref name="data"/> to the OnNext methods of each subscriber</summary>
/// <param name="data">Data object to send to subscribers</param>
/// <remarks>Uses delegate.BeginInvoke to send out notifications asynchronously.</remarks>
public void Notify(T data)
{
foreach (var observer in _observers)
{
delNext handler = observer.OnNext;
handler.BeginInvoke(data, null, null);
}
}
// delegate type for asynchronous invocation of IObserver.OnComplete method
private delegate void delComplete();
/// <summary>Notify all subscribers that the observable has completed</summary>
/// <remarks>Uses delegate.BeginInvoke to send out notifications asynchronously.</remarks>
public void NotifyComplete()
{
foreach (var observer in _observers)
{
delComplete handler = observer.OnCompleted;
handler.BeginInvoke(null, null);
}
}
}
Now you can create an Observable<byte[]> to use as your transmitter for Process<byte[]> instances that are interested. Pull data blocks out of the input stream, audio reader, etc. and pass them to the Notify method. Just make sure that you clone the arrays beforehand...
I was trying out some QueuedBackgroundWorker class that I found here. It works well, except I'm wondering how I can wait till all queued workers have finished? For example, if the user goes to close the program I'd like the program to wait until all workers are finished and then close.
I tried doing something like this on the GUI thread, but it just seems to block:
try
{
while (myWorkerQueue.Queue.Count > 0) ;
}
catch (InvalidOperationException)
{
}
Also tried while(myWorkerQueue.Queue.Peek() != null) and got same result.
Code for QueuedBackgroundWorker:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
/// <summary>
/// This is thread-safe
/// </summary>
public class QueuedBackgroundWorker
{
#region Constructors
public QueuedBackgroundWorker() { }
#endregion
#region Properties
Queue<object> Queue = new Queue<object>();
object lockingObject1 = new object();
public delegate void WorkerCompletedDelegate<K>(K result, Exception error);
#endregion
#region Methods
/// <summary>
/// doWork is a method with one argument
/// </summary>
/// <typeparam name="T">is the type of the input parameter</typeparam>
/// <typeparam name="K">is the type of the output result</typeparam>
/// <param name="inputArgument"></param>
/// <param name="doWork"></param>
/// <param name="workerCompleted"></param>
public void RunAsync<T,K>(Func<T, K> doWork, T inputArgument, WorkerCompletedDelegate<K> workerCompleted)
{
BackgroundWorker bw = GetBackgroundWorker<T,K>(doWork, workerCompleted);
Queue.Enqueue(new QueueItem(bw, inputArgument));
lock (lockingObject1)
{
if (Queue.Count == 1)
{
((QueueItem)this.Queue.Peek()).RunWorkerAsync();
}
}
}
/// <summary>
/// Use this method if you don't need to handle when the worker is completed
/// </summary>
/// <param name="doWork"></param>
/// <param name="inputArgument"></param>
public void RunAsync<T,K>(Func<T, K> doWork, T inputArgument)
{
RunAsync(doWork, inputArgument, null);
}
private BackgroundWorker GetBackgroundWorker<T, K>(Func<T, K> doWork, WorkerCompletedDelegate<K> workerCompleted)
{
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = false;
bw.WorkerSupportsCancellation = false;
bw.DoWork += (sender, args) =>
{
if (doWork != null)
{
args.Result = (K)doWork((T)args.Argument);
}
};
bw.RunWorkerCompleted += (sender, args) =>
{
if (workerCompleted != null)
{
workerCompleted((K)args.Result, args.Error);
}
Queue.Dequeue();
lock (lockingObject1)
{
if (Queue.Count > 0)
{
((QueueItem)this.Queue.Peek()).RunWorkerAsync();
}
}
};
return bw;
}
#endregion
}
public class QueueItem
{
#region Constructors
public QueueItem(BackgroundWorker backgroundWorker, object argument)
{
this.BackgroundWorker = backgroundWorker;
this.Argument = argument;
}
#endregion
#region Properties
public object Argument { get; private set; }
public BackgroundWorker BackgroundWorker { get; private set; }
#endregion
#region Methods
public void RunWorkerAsync()
{
this.BackgroundWorker.RunWorkerAsync(this.Argument);
}
#endregion
}
Do you absolutely have to use BackgroundWorker? .NET 4 introduces the Task API (aka Task Parallel Library or TPL in short): you can start several tasks and use Task.WhenAll to provide a continuation that only executes when all tasks are finished:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var myTasks = new List<Task<BitmapImage>>();
// Task<T>.Factory.StartNew starts the given method on a Thread Pool thread
myTasks.Add(Task<BitmapImage>.Factory.StartNew(LoadPicture1));
myTasks.Add(Task<BitmapImage>.Factory.StartNew(LoadPicture2));
// The important part: Task.WhenAll waits asynchronously until all tasks
// in the collection finished sucessfully. Only then, the lambda that is
// given to the ContinueWith method is executed. The UI thread does not block
// in this case.
Task.WhenAll(myTasks)
.ContinueWith(task =>
{
foreach (var bitmapImage in task.Result)
{
var image = new Image { Source = bitmapImage };
ImageStackPanel.Children.Add(image);
}
},
TaskScheduler.FromCurrentSynchronizationContext());
}
private BitmapImage LoadPicture1()
{
return LoadImageFile("Picture1.jpg");
}
private BitmapImage LoadPicture2()
{
// Simulate that this loading process takes a little bit longer
Thread.Sleep(1000);
return LoadImageFile("Picture2.jpg");
}
private BitmapImage LoadImageFile(string path)
{
using (var fileStream = new FileStream(path, FileMode.Open))
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = fileStream;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}
}
}
You even can use async await if you program against .NET 4.5 (but I doubt that you use this version of .NET because you provided the .NET 4.0 tag in your question). Anyway, in case you want to stick to several BackgroundWorker objects, I would encapsulate all of them in a class and register to the Completed events of them. If all of them raised this event, then I would raise another event that tells all of them have completed.
You can learn more about the TPL here: https://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx
You can download the whole example that I created here: https://dl.dropboxusercontent.com/u/14810011/LoadSeveralItemsWithTasks.zip
If you do something like that
while (myWorkerQueue.Queue.Count > 0) ;
Your while loop is taking so much ressource that there is no more for your background threads. It appears blocked.
If you want to keep your while loop (which I don't advise), at least put a sleep in so your background threads can work:
while (myWorkerQueue.Queue.Count > 0)
System.Threading.Thread.Sleep(1000);
The easiest solution as you said in your comments is to hook the closing event and abort it if myWorkerQueue.Queue.Count > 0.
A more elegant solution is to create a modal form with a progress bar, show it when the form is closing and if myWorkerQueue.Queue.Count > 0, the progress bar would progress as the remaining background worker finish...
The System.Threading.EventWaitHandle type does not contain a definition for 'dispose'.
How am I supposed to dispose of the EventWaitHandle when I want to kill the object in which it is contained? I've been having a serious memory leak problem and I've tried setting the EventWaitHandle to null and the memory leak persists.
Here's my code (I'm using Bob Craven's library, I added only the dispose() method):
using System;
using System.Threading;
namespace Rlc.Cron
{
public class CronObject
{
public delegate void CronEvent(CronObject cronObject);
public event CronEvent OnCronTrigger;
public event CronEvent OnStarted;
public event CronEvent OnStopped;
public event CronEvent OnThreadAbort;
private CronObjectDataContext _cronObjectDataContext;
private Guid _id = Guid.NewGuid();
private object _startStopLock = new object();
private EventWaitHandle _wh = new AutoResetEvent(false);
private Thread _thread;
public bool _isStarted;
private bool _isStopRequested;
private DateTime _nextCronTrigger;
public Guid Id { get { return _id; } }
public object Object { get { return _cronObjectDataContext.Object; } }
public DateTime LastTigger { get { return _cronObjectDataContext.LastTrigger; } }
/// <summary>
/// Initializes a new instance of the <see cref="CronObject"/> class.
/// </summary>
/// <param name="cronObjectDataContext">The cron object data context.</param>
public CronObject(CronObjectDataContext cronObjectDataContext)
{
if (cronObjectDataContext == null)
{
throw new ArgumentNullException("cronObjectDataContext");
}
if (cronObjectDataContext.Object == null)
{
throw new ArgumentException("cronObjectDataContext.Object");
}
if (cronObjectDataContext.CronSchedules == null || cronObjectDataContext.CronSchedules.Count == 0)
{
throw new ArgumentException("cronObjectDataContext.CronSchedules");
}
_cronObjectDataContext = cronObjectDataContext;
}
/// <summary>
/// Starts this instance.
/// </summary>
/// <returns></returns>
public bool Start()
{
lock (_startStopLock)
{
// Can't start if already started.
//
if (_isStarted)
{
return false;
}
_isStarted = true;
_isStopRequested = false;
// This is a long running process. Need to run on a thread
// outside the thread pool.
//
_thread = new Thread(ThreadRoutine);
_thread.Start();
}
// Raise the started event.
//
if(OnStarted != null)
{
OnStarted(this);
}
return true;
}
/// <summary>
/// Stops this instance.
/// </summary>
/// <returns></returns>
public bool Stop()
{
lock (_startStopLock)
{
// Can't stop if not started.
//
if (!_isStarted)
{
return false;
}
_isStarted = false;
_isStopRequested = true;
// Signal the thread to wake up early
//
_wh.Set();
// Wait for the thread to join.
//
if(!_thread.Join(5000))
{
_thread.Abort();
// Raise the thread abort event.
//
if(OnThreadAbort != null)
{
OnThreadAbort(this);
}
}
}
// Raise the stopped event.
//
if(OnStopped != null)
{
OnStopped(this);
}
return true;
}
public void dispose(){
this.Stop ();
this.OnCronTrigger = null;
this.OnStarted=null;
this.OnStopped=null;
this.OnThreadAbort=null;
this._cronObjectDataContext=null;
this._startStopLock = null;
this._wh = null;
this._thread=null;
}
/// <summary>
/// Cron object thread routine.
/// </summary>
private void ThreadRoutine()
{
// Continue until stop is requested.
//
while(!_isStopRequested)
{
// Determine the next cron trigger
//
DetermineNextCronTrigger(out _nextCronTrigger);
TimeSpan sleepSpan = _nextCronTrigger - DateTime.Now;
if(sleepSpan.TotalMilliseconds < 0)
{
// Next trigger is in the past. Trigger the right away.
//
sleepSpan = new TimeSpan(0, 0, 0, 0, 50);
}
// Wait here for the timespan or until I am triggered
// to wake up.
//
if(!_wh.WaitOne(sleepSpan))
{
// Timespan is up...raise the trigger event
//
if(OnCronTrigger != null)
{
OnCronTrigger(this);
}
// Update the last trigger time.
//
_cronObjectDataContext.LastTrigger = DateTime.Now;
}
}
}
/// <summary>
/// Determines the next cron trigger.
/// </summary>
/// <param name="nextTrigger">The next trigger.</param>
private void DetermineNextCronTrigger(out DateTime nextTrigger)
{
nextTrigger = DateTime.MaxValue;
foreach (CronSchedule cronSchedule in _cronObjectDataContext.CronSchedules)
{
DateTime thisTrigger;
if(cronSchedule.GetNext(LastTigger, out thisTrigger))
{
if (thisTrigger < nextTrigger)
{
nextTrigger = thisTrigger;
}
}
}
}
~CronObject(){
Console.WriteLine ("===================CRONOBJECT DESTROYED!!===============");
}
}
}
I need to design perfect worker thread method. The method must do the following:
1) extract something from queue (let's say a queue of string) and do something
2) stop and return when class is disposed
3) wait for some event (that queue is not empty) and do not consume cpu
4) run in separate thread
Main thread will add string to queue and signal thread method to continue and do the job.
I would like you to provide me the the template with required syncronization objects.
class MyClass, IDisposable
{
// Thread safe queue from third party
private ThreadSafeQueue<string> _workerQueue;
private Thread _workerThread;
public bool Initialize()
{
_workerThread = new Thread(WorkerThread).Start();
}
public AddTask(string object)
{
_workerQueue.Enqueue(object);
// now we must signal worker thread
}
// this is worker thread
private void WorkerThread()
{
// This is what worker thread must do
List<string> objectList = _workerQueue.EnqueAll
// Do something
}
// Yeap, this is Dispose
public bool Dispose()
{
}
}
Try something like this. instantiate with type string and give it a delegate to process your string:
public class SuperQueue<T> : IDisposable where T : class
{
readonly object _locker = new object();
readonly List<Thread> _workers;
readonly Queue<T> _taskQueue = new Queue<T>();
readonly Action<T> _dequeueAction;
/// <summary>
/// Initializes a new instance of the <see cref="SuperQueue{T}"/> class.
/// </summary>
/// <param name="workerCount">The worker count.</param>
/// <param name="dequeueAction">The dequeue action.</param>
public SuperQueue(int workerCount, Action<T> dequeueAction)
{
_dequeueAction = dequeueAction;
_workers = new List<Thread>(workerCount);
// Create and start a separate thread for each worker
for (int i = 0; i < workerCount; i++)
{
Thread t = new Thread(Consume) { IsBackground = true, Name = string.Format("SuperQueue worker {0}",i )};
_workers.Add(t);
t.Start();
}
}
/// <summary>
/// Enqueues the task.
/// </summary>
/// <param name="task">The task.</param>
public void EnqueueTask(T task)
{
lock (_locker)
{
_taskQueue.Enqueue(task);
Monitor.PulseAll(_locker);
}
}
/// <summary>
/// Consumes this instance.
/// </summary>
void Consume()
{
while (true)
{
T item;
lock (_locker)
{
while (_taskQueue.Count == 0) Monitor.Wait(_locker);
item = _taskQueue.Dequeue();
}
if (item == null) return;
// run actual method
_dequeueAction(item);
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
// Enqueue one null task per worker to make each exit.
_workers.ForEach(thread => EnqueueTask(null));
_workers.ForEach(thread => thread.Join());
}
}
What you are describing is best accomplished with the producer-consumer pattern. This pattern is most easily implemented with a blocking queue. If you are using .NET 4.0 then you can take advantage of the BlockingCollection class. Here is how I am seeing your code working. In the following example I am using a null value as sentinel for gracefully ending the consumer, but you could also take advantage of the CancellationToken parameter on the Take method.
public class MyClass : IDisposable
{
private BlockingCollection<string> m_Queue = new BlockingCollection<string>();
public class MyClass()
{
var thread = new Thread(Process);
thread.IsBackground = true;
thread.Start();
}
public void Dispose()
{
m_Queue.Add(null);
}
public void AddTask(string item)
{
if (item == null)
{
throw new ArgumentNullException();
}
m_Queue.Add(item);
}
private void Process()
{
while (true)
{
string item = m_Queue.Take();
if (item == null)
{
break; // Gracefully end the consumer thread.
}
else
{
// Process the item here.
}
}
}
}
I think you should consider using BackgroundWorker class, which may fit well to your needs.
Sounds like BlockingQueue is what you need.
You should take a look at the new .Net 4 System.Collections.Concurrent Namespace. Also this little example should help you to get a better understanding on how to use it.