I created a MvxTabBarViewController and when I'm trying open that using ShowViewModel method, a NullReferenceException occurs. The ViewModel property is always null and the exception occurs if I try instanciate that.
There is my MvxTabViewController
[Register("ProjectDetailsView")]
public class ProjectDetailsView : MvxTabBarViewController<ProjectDetailsViewModel>
{
private JVMenuPopoverViewController _menuController;
public override void ViewDidLoad()
{
base.ViewDidLoad();
if (ViewModel == null)
{
ViewModel = new ProjectDetailsViewModel();
}
CriaAbas();
CriaMenu();
}
private void CriaMenu()
{
var itensMenu = new List<JVMenuItem>();
var selectFavoriteProjectsItem = new JVMenuActionItem
{
Title = "Select Favorite Projects",
Command = () =>
{
var favoriteProjectsView = this.CreateViewControllerFor<FavoriteProjectsViewModel>() as UIViewController;
NavigationController.PushViewController(favoriteProjectsView, true);
}
};
itensMenu.Add(selectFavoriteProjectsItem);
var logoutItem = new JVMenuActionItem()
{
Title = "Logout",
Command = ViewModel.LogoutCommand.Execute
};
itensMenu.Add(logoutItem);
_menuController = new JVMenuPopoverViewController(itensMenu);
var menuButton = new UIBarButtonItem();
menuButton.Clicked += (sender, args) =>
{
_menuController.ShowMenuFromController(this);
};
menuButton.Image = UIImage.FromBundle("images/ic_menu_white_36pt.png");
NavigationItem.RightBarButtonItem = menuButton;
}
private void CriaAbas()
{
var abas = new List<UIViewController>();
var controller = new UINavigationController();
var informationView = this.CreateViewControllerFor(ViewModel.ProjectInformationViewModel) as UIViewController;
informationView.TabBarItem = new UITabBarItem("Information", UIImage.FromBundle("images/ic_info_outline.png"), 0);
informationView.Title = "Information";
controller.PushViewController(informationView, false);
abas.Add(informationView);
controller = new UINavigationController();
var milestonesView = this.CreateViewControllerFor(ViewModel.ProjectMilestonesViewModel) as UIViewController;
milestonesView.TabBarItem = new UITabBarItem("Milestones", UIImage.FromBundle("images/ic_assistant_photo_48pt.png"), 0);
milestonesView.Title = "Milestones";
controller.PushViewController(milestonesView, false);
abas.Add(milestonesView);
ViewControllers = abas.ToArray();
SelectedViewController = ViewControllers[0];
}
}
I'm trying to open this view calling ShowViewModel();
What I'm doing wrong?
there is a part of my stack trace:
mvx: Diagnostic: 72,18 Showing ViewModel ProjectDetailsViewModel
iOSNavigation: Diagnostic: 72,18 Navigate requested
Unhandled Exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object
at MvvmCross.iOS.Views.MvxTabBarViewController.set_DataContext (System.Object value) [0x00001] in C:\vcs\git\MvvmCross\MvvmCross\iOS\iOS\Views\MvxTabBarViewController.cs:38
at MvvmCross.iOS.Views.MvxTabBarViewController.set_ViewModel (IMvxViewModel value) [0x00001] in C:\vcs\git\MvvmCross\MvvmCross\iOS\iOS\Views\MvxTabBarViewController.cs:44
at MvvmCross.iOS.Views.MvxTabBarViewController`1[TViewModel].set_ViewModel (MvvmCross.iOS.Views.TViewModel value) [0x00001] in C:\vcs\git\MvvmCross\MvvmCross\iOS\iOS\Views\MvxTabBarViewController.cs:68
at PROSPERI_EPMFast.iOS.Views.ProjectDetailsView.ViewDidLoad () [0x00016] in C:\Projetos Vinicius\PROSPERI_EPMFast\PROSPERI_EPMFast.iOS\Views\ProjectDetailsView.cs:22
at (wrapper managed-to-native) ObjCRuntime.Messaging:IntPtr_objc_msgSendSuper (intptr,intptr)
at UIKit.UITabBarController..ctor () [0x0003b] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/src/build/ios/native/UIKit/UITabBarController.g.cs:54
at MvvmCross.Platform.iOS.Views.MvxEventSourceTabBarController..ctor () [0x00000] in C:\vcs\git\MvvmCross\MvvmCross\Platform\iOS\Views\MvxEventSourceTabBarController.cs:20
at MvvmCross.iOS.Views.MvxTabBarViewController..ctor () [0x00000] in C:\vcs\git\MvvmCross\MvvmCross\iOS\iOS\Views\MvxTabBarViewController.cs:20
at MvvmCross.iOS.Views.MvxTabBarViewController`1[TViewModel]..ctor () [0x00000] in C:\vcs\git\MvvmCross\MvvmCross\iOS\iOS\Views\MvxTabBarViewController.cs:56
at PROSPERI_EPMFast.iOS.Views.ProjectDetailsView..ctor () <0x1b486900 + 0x0002b> in <filename unknown>:0
at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:644
For the sake of completion: https://stackoverflow.com/a/30283563/6143949
I'm guessing here the problem here will be specific to the way that
TabBarViewController is constructed.
ViewDidLoad is a virtual method and it is called the first time the
View is accessed.
In the case of TabBarViewController this happens during the iOS base
View constructor - i.e. it occurs before the class itself has had its
constructor called.
The only way around this I've found is to add a check against the
situation in ViewDidLoad, and to make a second call to ViewDidLoad
during the class constructor.
You can see this in action N-25 -
https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/976ede3aafd3a7c6e06717ee48a9a45f08eedcd0/N-25-Tabbed/Tabbed.Touch/Views/FirstView.cs#L17
Something like:
public class MainView : MvxTabBarViewController
{
private bool _constructed;
public MainView()
{
_constructed = true;
// need this additional call to ViewDidLoad because UIkit creates the view before the C# hierarchy has been constructed
ViewDidLoad();
}
public override void ViewDidLoad()
{
if (!_constructed)
return;
base.ViewDidLoad();
var vm = (MainViewModel)this.ViewModel;
if (vm == null)
return;
}
}
Solution provided by Stuart (https://stackoverflow.com/users/373321/stuart)
Related
using MVC jQuery .NET Core
using Abp Background Job
I have a background job that get Enqueued correctly and I can see the job in the db.
However the logging shows me I'm getting and authorization error (ee below) despite being logged in. I have the same AbpAuthorize attributes placed on the services and background jobs.
Not sure how to proceed debugging the problem - any suggestions welcome.
SERVICE CODE USING/ENQUEUING THE JOB:
public async Task BulkImportBackground(IList<CreatePracticeDto> inputs)
{
await _backgroundJobManager.EnqueueAsync<ImportBulkPracticesBackgroundJob, ImportBulkPracticesBackgroundJobArgs>(
new ImportBulkPracticesBackgroundJobArgs
{
CreatePracticeDtos = inputs
});
_backgroundJobManager.Start();
}
JOB CLASS CODE:
using Abp.Authorization;
using Abp.BackgroundJobs;
using Abp.Dependency;
using Abp.Modules;
using Gp.Authorization;
using Gp.Ccre.ImportResult;
using Gp.Ccre.ImportResult.Dtos;
using Gp.Ccre.Practice.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using Abp.Domain.Uow;
namespace Gp.Ccre.Practice.BackgroundJobs
{
[DependsOn(
typeof(IPracticeAppService),
typeof(IImportResultsAppService))]
[AbpAuthorize(PermissionNames.Pages_Practices, RequireAllPermissions = false)]
public class ImportBulkPracticesBackgroundJob : BackgroundJob<ImportBulkPracticesBackgroundJobArgs>, ITransientDependency
{
private readonly IPracticeAppService _practiceAppService;
private readonly IImportResultsAppService _importResultsAppService;
public ImportBulkPracticesBackgroundJob(IImportResultsAppService importResultsAppService, IPracticeAppService practiceAppService)
{
_practiceAppService = practiceAppService;
_importResultsAppService = importResultsAppService;
}
public override void Execute(ImportBulkPracticesBackgroundJobArgs args)
{
IList<CreateImportResultDto> createResultsDto = new List<CreateImportResultDto>();
var k = 0;
DateTime importedOn = DateTime.Now;
foreach (CreatePracticeDto createDto in args.CreatePracticeDtos)
{
k++;
try
{
//this correctly automatically ignores 'soft' deleted records.
var count = _practiceAppService.GetAllIdName().Result.Count(x => x.Name.Equals(createDto.Name, StringComparison.CurrentCultureIgnoreCase));
if (count > 0)
{
createResultsDto.Add(new CreateImportResultDto
{
Row = k,
Name = createDto.Name,
Message = "Already exists. Skipped.",
MessageType = AppConsts.ImportMessageType.Warning.ToString(),
ImportedOn = importedOn
});
continue;
}
// ** GOOGLE MAP API CALL **
var coordinatesFound = false; //_practiceAppService.TryGetGeoCodeAddress(createDto);
createDto.ImportedOn = importedOn;
// *** SAVE SINGLE ****
_practiceAppService.Create(createDto);
createResultsDto.Add(new CreateImportResultDto
{
Row = k,
Name = createDto.Name,
Message = "Successful.",
MessageType = AppConsts.ImportMessageType.Info.ToString(),
ImportedOn = importedOn,
GoogleMapCoordinatesFound = coordinatesFound
});
}
catch (Exception e)
{
createResultsDto.Add(new CreateImportResultDto
{
Row = k,
Name = createDto.Name,
Message = e.Message,
MessageType = AppConsts.ImportMessageType.Error.ToString(),
ImportedOn = importedOn
});
continue;
}
}
//*** SAVE RESULTS ***
foreach (var resultDto in createResultsDto)
{
_importResultsAppService.Create(resultDto);
}
CurrentUnitOfWork.SaveChanges();
}
}
[Serializable]
public class ImportBulkPracticesBackgroundJobArgs
{
public IList<CreatePracticeDto> CreatePracticeDtos { get; set; }
}
}
STACK TRACE (partial)
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Abp.Authorization.AbpAuthorizationException: Current user did not login to the application!
at Abp.Authorization.AuthorizationHelper.AuthorizeAsync(IEnumerable`1 authorizeAttributes) in D:\Github\aspnetboilerplate\src\Abp\Authorization\AuthorizationHelper.cs:line 43
at Abp.Authorization.AuthorizationHelper.CheckPermissions(MethodInfo methodInfo, Type type) in D:\Github\aspnetboilerplate\src\Abp\Authorization\AuthorizationHelper.cs:line 98
at Abp.Authorization.AuthorizationHelper.AuthorizeAsync(MethodInfo methodInfo, Type type) in D:\Github\aspnetboilerplate\src\Abp\Authorization\AuthorizationHelper.cs:line 57
at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task)
at Nito.AsyncEx.AsyncContext.Run(Func`1 action)
at Abp.Authorization.AuthorizationInterceptor.Intercept(IInvocation invocation) in D:\Github\aspnetboilerplate\src\Abp\Authorization\AuthorizationInterceptor.cs:line 20
at Castle.DynamicProxy.AbstractInvocation.Proceed()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Abp.BackgroundJobs.BackgroundJobManager.TryProcessJob(BackgroundJobInfo jobInfo) in D:\Github\aspnetboilerplate\src\Abp\BackgroundJobs\BackgroundJobManager.cs:line 119
The problem was caused by trying to inject a service into the background job. stick to IRepository and all good.
for my app i create my own buttons using a frame and adding a tapgesture to it. here i use the navigation of prism to go to a specific page with a parameter. however. the viewmodel i'm going to does not trigger the Navigated to method. here is some code.
during debugging it seems that the adding of the parameters is no problem. however the constructor for the viewmodel is called instead.
button
public class FolderButton : Frame
{
public FolderButton(Folder folder, INavigationService navigationService)
{
var navParams = new NavigationParameters();
navParams.Add("folder", folder);
GestureRecognizers.Add(new TapGestureRecognizer()
{
Command = new Command(async () => { await navigationService.NavigateAsync("FolderInventory", navParams); }),
});
BackgroundColor = Color.CornflowerBlue;
var thickness = new Thickness();
thickness.Bottom = 10;
thickness.Left = 10;
thickness.Right = 10;
thickness.Top = 10;
Margin = thickness;
CornerRadius = 5;
var completeStack = new StackLayout();
var imgStack = new StackLayout();
imgStack.Padding = thickness;
imgStack.Children.Add(new Image { Source = "folder.png" });
completeStack.Children.Add(imgStack);
var lblStack = new StackLayout();
lblStack.Padding = thickness;
lblStack.Children.Add(new Label
{
Text = folder.Name,
HorizontalTextAlignment = TextAlignment.Center,
VerticalTextAlignment = TextAlignment.Start
});
completeStack.Children.Add(lblStack);
Content = completeStack;
}
}
called viewmodel
public class FolderInventoryViewModel : BindableBase, INavigatedAware
{
public Folder Folder => _folder;
private readonly INavigationService _navigationService;
private Folder _folder;
private readonly ISQLiteService _sqlService;
private List<Frame> _buttons;
public List<Frame> Buttons
{
get => _buttons;
set => _buttons = value;
}
public FolderInventoryViewModel(Folder folder, INavigationService navigationService, ISQLiteService sqlService)
{
_folder = folder;
_sqlService = sqlService;
_navigationService = navigationService;
GetItemsForFolder();
}
private void GetItemsForFolder()
{
var itemList = _sqlService.GetAllFolderItems(Folder.Name);
foreach (var item in itemList)
{
var itemButton = new ItemButton(_navigationService, item);
_buttons.Add(itemButton);
}
}
public void OnNavigatedFrom(NavigationParameters parameters)
{
if (parameters["folder"] is Folder folder)
{
_folder = folder;
}
}
public void OnNavigatedTo(NavigationParameters parameters)
{
if (parameters["folder"] is Folder folder)
{
_folder = folder;
}
}
}
This is not the essence of using the framework. To properly use the Prism with its NavigationParameters you first properly maintain the MVVM idea behind it.
E.g.
<Button Command="{Binding testCommand}" text="TestButton"/>
Your ViewModel (Pardon about this, you need to inject NavigationService to your ViewModel's constructor)
private DelegateCommand _testCommand;
public DelegateCommand testCommand =>
_testCommand?? (_testCommand= new DelegateCommand(ExecuteTest));
private void ExecuteTest()
{
NavigationParameters navigationParameters = new NavigationParameters();
navigationParameters.Add("yourParameterId", value);
NavigationService.NavigateAsync("YourPage", navigationParameters);
}
And then onto your next page
Inherit INavigationAware to your NextPage : YourNextPage: BaseViewModel, INavigationAware
INavigationAware has 3 methods NavigatingTo, NavigatedTo, NavigatedFrom
Inside OnNavigatedTo you can call the parameters you have passed
public void OnNavigatedTo(NavigationParameters parameters)
{
//You can check if parameters has value before passing it inside
if(parameters.ContainsKey("YourParameterId")
{
yourItem = (dataType)parameters[YourParameterId];
}
}
Also note: The constructor will always be called first before the Navigating methods
when we are trying update realm db multiple times its throwing following error
NHANDLED EXCEPTION:
System.ObjectDisposedException: Safe handle has been closed
at (wrapper managed-to-native) System.Object:wrapper_native_0xda0c96a0 (intptr)
at Realms.SynchronizationContextEventLoopSignal+EventLoop+<Post>c__AnonStorey0.<>m__0 (System.Object _) [0x00012] in <550621f4184f47c0bdfce087c391c293>:0
at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <d855bac285f44dda8a0d8510b679b1e2>:0
at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <d855bac285f44dda8a0d8510b679b1e2>:0
at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <d855bac285f44dda8a0d8510b679b1e2>:0
at (wrapper dynamic-method) System.Object:f9edc18f-c720-423d-8576-23c20d8f6a89 (intptr,intptr)
we have tried to keep our code inside try and catch but its still throwing runtime error and application gets crashed
Code , here EventType is a normal class ( not derived from RealmObject ) but Response class is a RealmObject and it has opportunitiesList declared as
public IList<Opportunity> opportunitiesList { get; } and its derived from RealmObject
private void updateGetData(EventType e)
{
try
{
Realm realm = Realm.GetInstance();
realm.WriteAsync(tempRealm =>
{
if (Settings.IsSingleOpportunityUpdated == false)
{
tempRealm.Add(e.response, true);
}
else if (Settings.IsSingleOpportunityUpdated)
{
if (e.response != null && e.response.opportunitiesList.Count > 0)
{
long opportunityId = e.response.opportunitiesList[0].opportunityId;
var opportunity = tempRealm.All<Opportunity>().FirstOrDefault(d => d.opportunityId == opportunityId);
e.response.opportunitiesList[0].isOpportunitySync = 0;
e.response.opportunitiesList[0].isSync = SyncStatus.SYNCED;
e.response.opportunitiesList[0].mOpportunityId = opportunity.mOpportunityId;
Debug.WriteLine("Opportunity Id after getOpportunity " + opportunity.opportunityId + " " + opportunityId + " " + e.response.opportunitiesList[0].isSync);
tempRealm.Add(e.response.opportunitiesList[0], true);
}
else
{
Debug.WriteLine("RealmDB opportunity response is empty " + Settings.IsSingleOpportunityUpdated);
}
}
});
}
catch(Exception exception)
{
Debug.WriteLine("RealmDB " + exception.StackTrace);
}
}
I'm stuck on getting my DocumentPicker fully working. Right now it presents the view controller but I can't figure out how to wait or get the result.
In swift you just write the void documentPicker(UIDocumentPickerViewController controller, didPickDocumentAtUrl... method and when it's finished it goes to there.
But in Xamarin it must not be that simple. I've written that method, from the class I'm calling it from as well as in my AppDelegate.cs class and as well as in my Main.cs class. None seem to work, unless I've written it wrong.
What I have is this ....
public async Task<string> pickResume()
{
string path = string.Empty;
var controller = new UIViewController();
var docVC = new UIDocumentPickerViewController(new string[] { "org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc" }, UIDocumentPickerMode.Import);
UIViewController topController = getTopViewController();
topController.PresentViewController(docVC, true, null);
return path;
}
void documentPicker(UIDocumentPickerViewController controller, NSUrl didPickDocumentAtURL)
{
Console.WriteLine("done");
}
getTopViewController() is just a helper method to get the top view controller so I can present the DocumentPicker
Figured it out, and it's a lot easier than I was making it out to be.
The UIDocumentPickerViewController has two EventHandlers, DidPickDocument and WasCancelled so I just assigned those to two different methods and done.
public async Task<string> pickResume()
{
string path = string.Empty;
var controller = new UIViewController();
var docVC = new UIDocumentPickerViewController(new string[] { "org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc" }, UIDocumentPickerMode.Import);
docVC.DidPickDocument += DocVC_DidPickDocument;
docVC.WasCancelled += DocVC_WasCancelled;
UIViewController topController = getTopViewController();
topController.PresentViewController(docVC, true, null);
return await GetDocPath(new CancellationTokenSource());
}
private void DocVC_WasCancelled(object sender, EventArgs e)
{
//Handle being cancelled
}
private void DocVC_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
{
//Handle document selection
}
In rx you can write :
var oe = Observable.FromEventPattern<SqlNotificationEventArgs>(sqlDep, "OnChange");
and then subscribe to the observable to convert the OnChange event on the sqlDep object into an observable.
Similarily, how can you create a Task from a C# event using the Task Parallel Library ?
EDIT: clarification
The solution pointed by Drew and then written explicitely by user375487 works for a single event. As soon as the task finished ... well it is finished.
The observable event is able to trigger again at any time. It is can be seen as an observable stream. A kind of ISourceBlock in the TPL Dataflow. But in the doc http://msdn.microsoft.com/en-us/library/hh228603(v=vs.110).aspx there is no example of ISourceBlock.
I eventually found a forum post explaining how to do that: http://social.msdn.microsoft.com/Forums/en/tpldataflow/thread/a10c4cb6-868e-41c5-b8cf-d122b514db0e
public static ISourceBlock CreateSourceBlock(
Action,Action,Action,ISourceBlock> executor)
{
var bb = new BufferBlock();
executor(t => bb.Post(t), () => bb.Complete(), e => bb.Fault(e), bb);
return bb;
}
//Remark the async delegate which defers the subscription to the hot source.
var sourceBlock = CreateSourceBlock<SomeArgs>(async (post, complete, fault, bb) =>
{
var eventHandlerToSource = (s,args) => post(args);
publisher.OnEvent += eventHandlerToSource;
bb.Complete.ContinueWith(_ => publisher.OnEvent -= eventHandlerToSource);
});
I've not tryed the above code. There may be a mismatch between the async delegate and the definition of CreateSourceBlock.
There is no direct equivalent for the Event Asynchronous Pattern (EAP) baked into the TPL. What you need to do is using a TaskCompletionSource<T> that you signal yourself in the event handler. Check out this section on MSDN for an example of what that would look like which uses WebClient::DownloadStringAsync to demonstrate the pattern.
You can use TaskCompletionSource.
public static class TaskFromEvent
{
public static Task<TArgs> Create<TArgs>(object obj, string eventName)
where TArgs : EventArgs
{
var completionSource = new TaskCompletionSource<TArgs>();
EventHandler<TArgs> handler = null;
handler = new EventHandler<TArgs>((sender, args) =>
{
completionSource.SetResult(args);
obj.GetType().GetEvent(eventName).RemoveEventHandler(obj, handler);
});
obj.GetType().GetEvent(eventName).AddEventHandler(obj, handler);
return completionSource.Task;
}
}
Example usage:
public class Publisher
{
public event EventHandler<EventArgs> Event;
public void FireEvent()
{
if (this.Event != null)
Event(this, new EventArgs());
}
}
class Program
{
static void Main(string[] args)
{
Publisher publisher = new Publisher();
var task = TaskFromEvent.Create<EventArgs>(publisher, "Event").ContinueWith(e => Console.WriteLine("The event has fired."));
publisher.FireEvent();
Console.ReadKey();
}
}
EDIT Based on your clarification, here is an example of how to achieve your goal with TPL DataFlow.
public class EventSource
{
public static ISourceBlock<TArgs> Create<TArgs>(object obj, string eventName)
where TArgs : EventArgs
{
BufferBlock<TArgs> buffer = new BufferBlock<TArgs>();
EventHandler<TArgs> handler = null;
handler = new EventHandler<TArgs>((sender, args) =>
{
buffer.Post(args);
});
buffer.Completion.ContinueWith(c =>
{
Console.WriteLine("Unsubscribed from event");
obj.GetType().GetEvent(eventName).RemoveEventHandler(obj, handler);
});
obj.GetType().GetEvent(eventName).AddEventHandler(obj, handler);
return buffer;
}
}
public class Publisher
{
public event EventHandler<EventArgs> Event;
public void FireEvent()
{
if (this.Event != null)
Event(this, new EventArgs());
}
}
class Program
{
static void Main(string[] args)
{
var publisher = new Publisher();
var source = EventSource.Create<EventArgs>(publisher, "Event");
source.LinkTo(new ActionBlock<EventArgs>(e => Console.WriteLine("New event!")));
Console.WriteLine("Type 'q' to exit");
char key = (char)0;
while (true)
{
key = Console.ReadKey().KeyChar;
Console.WriteLine();
if (key == 'q') break;
publisher.FireEvent();
}
source.Complete();
Console.ReadKey();
}
}