UWP Timer Trigger Background Task Get's Suspended - windows

I am working on BackgroundTask on UWP and have been trying to run this task but for some unknown reason it always ends up showing suspended in Task Manager and doesn't run. Below is the code
This how I am registering my BackgroundTask
public static async Task InitializeLatestNewsTaskAsync()
{
TimeTrigger hourlyTrigger = new TimeTrigger(15, false);
BackgroundExecutionManager.RemoveAccess();
var requestStatus = await BackgroundExecutionManager
.RequestAccessKindAsync(BackgroundAccessRequestKind.AlwaysAllowed, "Please allow the app to run in background");
if (!requestStatus)
{
Util.MessageBox("Warrning", "You will not receive any Announcements notifications");
}
else
{
try
{
BackgroundTaskRegistration backgroundTaskRegistration = RegisterBackgroundTask(Constants.BACKGROUND_TASK_LATEST_NEWS_ENTRY_POINT,
Constants.BACKGROUND_TASK_LATEST_NEWS_NAME, hourlyTrigger, null);
}
catch (Exception e)
{
log.Error(e.Message, e);
}
}
}
//
// Register a background task with the specified taskEntryPoint, name, trigger,
// and condition (optional).
//
// taskEntryPoint: Task entry point for the background task.
// taskName: A name for the background task.
// trigger: The trigger for the background task.
// condition: Optional parameter. A conditional event that must be true for the task to fire.
//
private static BackgroundTaskRegistration RegisterBackgroundTask(
string taskEntryPoint, string taskName, IBackgroundTrigger trigger, IBackgroundCondition condition)
{
//
// Check for existing registrations of this background task.
//
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
if (cur.Value.Name == taskName)
{
//
// The task is already registered.
//
BackgroundTaskRegistration backgroundTaskRegistration = (BackgroundTaskRegistration)(cur.Value);
backgroundTaskRegistration.Unregister(true);
break;
}
}
//
// Register the background task.
//
var builder = new BackgroundTaskBuilder();
builder.Name = taskName;
builder.TaskEntryPoint = taskEntryPoint;
builder.SetTrigger(trigger);
builder.IsNetworkRequested = true;
if (condition != null)
{
builder.AddCondition(condition);
}
BackgroundTaskRegistration task = builder.Register();
task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
return task;
}
private static async void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
{
log.Info("Completed");
}
And Here is my Package.appxmanifest Declarations
and My BackgroundTask Implementation
public sealed class LatestNewsBackgroundTask : IBackgroundTask
{
private ILogger log = LogManagerFactory.DefaultLogManager.GetLogger<LatestNewsBackgroundTask>();
BackgroundTaskDeferral _deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
log.Info("Latest New Background Task Started");
_deferral = taskInstance.GetDeferral();
taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
var announcementTypes = localSettings.Values["announcementTypes"];
NotificationHelper.GenerateToast("working from background task ", announcementTypes.ToString());
log.Info("Latest New Background Task Completed");
_deferral.Complete();
}
private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
// Indicate that the background task is canceled.
log.Info("Background " + sender.Task.Name + " Cancel Requested...");
}
}
What all I have tried to debug
https://learn.microsoft.com/en-us/windows/uwp/launch-resume/debug-a-background-task

Related

ZKEmkeeper: Events not triggering on Windows Service

I'm stucked for a while trying to use zkemkeeper sdk to use on a Windows Service that uses a InBios(Controller) for fingerprint.
i first connect to the device and then i add the event OnAttTransactionEx, someone can point me what i'm doing wrong.
Here is the code snippet
`
protected override void OnStart(string[] args)
{
Thread TT = new Thread(new ThreadStart(WorkedThread));
TT.IsBackground = true;
TT.SetApartmentState(ApartmentState.STA);
this.isServiceStarted = true;
TT.Start();
}
private void WorkedThread()
{
WriteToFile("Worker Thread Started.");
ZKemClient objZkeeper = new ZKemClient(filepath);
this.isDeviceConnected = objZkeeper.Connect_Net("19x.x.x.24x", 4370);
if (this.isDeviceConnected)
{
WriteToFile("Device connected.");
WriteToFile("While loop execution starting.");
while (true)
{
WriteToFile(filepath, "While loop execution started.");
}
}
else
{
WriteToFile("Failed to connect to Device.");
}
}
// ZMClient class
public bool Connect_Net(string IPAdd, int Port)
{
bool bResult = false;
try
{
// Actual SDK class
CZKEM objCZKEM = new CZKEM();
if (objCZKEM.Connect_Net(IPAdd, Port))
{
if (objCZKEM.RegEvent(1, 32767))
{
// [ Register your events here ]
objCZKEM.OnAttTransactionEx += new _IZKEMEvents_OnAttTransactionExEventHandler(zkemClient_OnAttTransactionEx);
}
bResult = true;
}
}
catch (Exception ex)
{
WriteToFile("Connect_Net() Exception->" + ex.Message);
}
return bResult;
}
`
After playing a lot with threading, found a solution.
For windows service - Add reference to System.Windows.Forms
Thread TT1 = new Thread(() =>
{
this.objCZKEM = new CZKEM();
Application.Run();
});
TT1.IsBackground = true;
TT1.SetApartmentState(ApartmentState.STA);
TT1.Start();
Simply continue with current thread
if (this.objCZKEM.Connect_Net(IP, 4370))
{
this.WriteToFile("ZKEM device connected");
if (this.objCZKEM.RegEvent(1, 32767))
{
this.WriteToFile("ZKEM device events registration started");
// [ Register your events here ]
this.objCZKEM.OnAttTransactionEx += new _IZKEMEvents_OnAttTransactionExEventHandler(zkemClient_OnAttTransactionEx);
this.WriteToFile("Done with ZKEM device events registration.");
}
You should not able to receive finger events from device.

Using UIDocumentPickerViewController in Xamarin forms as a dependency service

I'm using Xamarin forms and writing a dependency service for the following objectives :
Open iOS files app. (UIDocumentPickerViewController )
Select any kind of a document.
Copy that document into my application Documents directory. (For app access)
Show that document into my application by storing its path into my SQLite DB.
What I am trying to do here is call the Files app from my application on an Entry click and the click event seems to be working well my dependency service calls perfectly but now when I try to use the UIDocumentPickerViewController I am unable to get View controller context in my dependency service to call the PresentViewController method. Now I know about the xamarin forms context but I don't know if it will work here and I don't even know if it would be a smart idea to use it as it has already been marked as obsolete and since I am not from the iOS background, I don't know what would be the right solution for it.
My code is as follows :
public class DocumentPickerRenderer : IDocumentPicker
{
public object PickFile()
{
var docPicker = new UIDocumentPickerViewController(new string[] { UTType.Data, UTType.Content }, UIDocumentPickerMode.Import);
docPicker.WasCancelled += (sender, wasCancelledArgs) =>
{
};
docPicker.DidPickDocumentAtUrls += (object sender, UIDocumentPickedAtUrlsEventArgs e) =>
{
Console.WriteLine("url = {0}", e.Urls[0].AbsoluteString);
//bool success = await MoveFileToApp(didPickDocArgs.Url);
var success = true;
string filename = e.Urls[0].LastPathComponent;
string msg = success ? string.Format("Successfully imported file '{0}'", filename) : string.Format("Failed to import file '{0}'", filename);
var alertController = UIAlertController.Create("import", msg, UIAlertControllerStyle.Alert);
var okButton = UIAlertAction.Create("OK", UIAlertActionStyle.Default, (obj) =>
{
alertController.DismissViewController(true, null);
});
alertController.AddAction(okButton);
PresentViewController(alertController, true, null);
};
PresentViewController(docPicker, true, null);
}
}
My questions:
Is my methodology correct for picking files?
what will be the object that I will be getting as a callback from a file selection and how will I get the callback?
Is there any other way or something available for xamarin forms, some guide or something that allows me to pick documents from my native file systems and gives a brief on how to handle it in both ios and android?
Hello Guys, You can use following code for picking any type of documents to mention in code using iOS Devices-
use follwing interface:
public interface IMedia
{
Task<string> OpenDocument();
}
public Task<string> OpenDocument()
{
var task = new TaskCompletionSource<string>();
try
{
OpenDoc(GetController(), (obj) =>
{
if (obj == null)
{
task.SetResult(null);
return;
}
var aa = obj.AbsoluteUrl;
task.SetResult(aa.Path);
});
}
catch (Exception ex)
{
task.SetException(ex);
}
return task.Task;
}
static Action<NSUrl> _callbackDoc;
public static void OpenDoc(UIViewController parent, Action<NSUrl> callback)
{
_callbackDoc = callback;
var version = UIDevice.CurrentDevice.SystemVersion;
int verNum = 0;
Int32.TryParse(version.Substring(0, 2), out verNum);
var allowedUTIs = new string[]
{
UTType.UTF8PlainText,
UTType.PlainText,
UTType.RTF,
UTType.PNG,
UTType.Text,
UTType.PDF,
UTType.Image,
UTType.Spreadsheet,
"com.microsoft.word.doc",
"org.openxmlformats.wordprocessingml.document",
"com.microsoft.powerpoint.ppt",
"org.openxmlformats.spreadsheetml.sheet",
"org.openxmlformats.presentationml.presentation",
"com.microsoft.excel.xls",
};
// Display the picker
var pickerMenu = new UIDocumentMenuViewController(allowedUTIs, UIDocumentPickerMode.Import);
pickerMenu.DidPickDocumentPicker += (sender, args) =>
{
if (verNum < 11)
{
args.DocumentPicker.DidPickDocument += (sndr, pArgs) =>
{
UIApplication.SharedApplication.OpenUrl(pArgs.Url);
pArgs.Url.StopAccessingSecurityScopedResource();
var cb = _callbackDoc;
_callbackDoc = null;
pickerMenu.DismissModalViewController(true);
cb(pArgs.Url.AbsoluteUrl);
};
}
else
{
args.DocumentPicker.DidPickDocumentAtUrls += (sndr, pArgs) =>
{
UIApplication.SharedApplication.OpenUrl(pArgs.Urls[0]);
pArgs.Urls[0].StopAccessingSecurityScopedResource();
var cb = _callbackDoc;
_callbackDoc = null;
pickerMenu.DismissModalViewController(true);
cb(pArgs.Urls[0].AbsoluteUrl);
};
}
// Display the document picker
parent.PresentViewController(args.DocumentPicker, true, null);
};
pickerMenu.ModalPresentationStyle = UIModalPresentationStyle.Popover;
parent.PresentViewController(pickerMenu, true, null);
UIPopoverPresentationController presentationPopover = pickerMenu.PopoverPresentationController;
if (presentationPopover != null)
{
presentationPopover.SourceView = parent.View;
presentationPopover.PermittedArrowDirections = UIPopoverArrowDirection.Down;
}
}
Now you need to call using following code:
var filePath = await DependencyService.Get<IMedia>().OpenDocument();
For pick document in Android, you can use following code
public class IntentHelper
{
public const int DocPicker = 101;
static Action<string> _callback;
public static async void ActivityResult(int requestCode, Result resultCode, Intent data)
{ if (requestCode == RequestCodes.DocPicker)
{
if (data.Data == null)
{
_callback(null);
}
else
{
var destFilePath = FilePath.GetPath(CurrentActivity, data.Data);
_callback(destFilePath);
}
}
}
public static Activity CurrentActivity
{
get
{
return (Xamarin.Forms.Forms.Context as MainActivity);
}
}
public static void OpenDocPicker(Action<string> callback)
{
_callback = callback;
var intent = new Intent(Intent.ActionOpenDocument);
intent.AddCategory(Intent.CategoryOpenable);
intent.SetType("*/*");
CurrentActivity.StartActivityForResult(intent, RequestCodes.DocPicker);
}
}
For pick document in Android, you can use following code:
public class IntentHelper
{
public const int DocPicker = 101;
static Action<string> _callback;
public static async void ActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == RequestCodes.DocPicker)
{
if (data.Data == null)
{
_callback(null);
}
else
{
var destFilePath = FilePath.GetPath(CurrentActivity, data.Data);
_callback(destFilePath);
}
}
}
public static Activity CurrentActivity
{
get
{
return (Xamarin.Forms.Forms.Context as MainActivity);
}
}
public static void OpenDocPicker(Action<string> callback)
{
_callback = callback;
var intent = new Intent(Intent.ActionOpenDocument);
intent.AddCategory(Intent.CategoryOpenable);
intent.SetType("*/*");
CurrentActivity.StartActivityForResult(intent, RequestCodes.DocPicker);
}
}
Use below code to access the helper class: public class Media:
IMedia {
public Task<string> OpenDocument() {
var task = new TaskCompletionSource<string>();
try {
IntentHelper.OpenDocPicker((path) => { task.SetResult(path); });
} catch (Exception ex) {
task.SetResult(null);
}
return task.Task;
}
}
Since I was looking for UIDocumentPickerViewController and not UIDocumentMenuViewController the other answer was not what I was looking for :
So this is how I ended up doing it:
Calling the document picker:
var docPicker = new UIDocumentPickerViewController(new string[]
{ UTType.Data, UTType.Content }, UIDocumentPickerMode.Import);
docPicker.WasCancelled += DocPicker_WasCancelled;
docPicker.DidPickDocumentAtUrls += DocPicker_DidPickDocumentAtUrls;
docPicker.DidPickDocument += DocPicker_DidPickDocument;
var _currentViewController = GetCurrentUIController();
if (_currentViewController != null)
_currentViewController.PresentViewController(docPicker, true, null);
Where GetCurrentUIController is the function to get the current UI controller something like this :
public UIViewController GetCurrentUIController()
{
UIViewController viewController;
var window = UIApplication.SharedApplication.KeyWindow;
if (window == null)
{
return null;
}
if (window.RootViewController.PresentedViewController == null)
{
window = UIApplication.SharedApplication.Windows
.First(i => i.RootViewController != null &&
i.RootViewController.GetType().FullName
.Contains(typeof(Xamarin.Forms.Platform.iOS.Platform).FullName));
}
viewController = window.RootViewController;
while (viewController.PresentedViewController != null)
{
viewController = viewController.PresentedViewController;
}
return viewController;
}
For below iOS 11 i added the DidPickDocument event:
private void DocPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
{
try
{
NSUrl filePath = e.Url.AbsoluteUrl;
//This is the url for your document and you can use it as you please.
}
catch (Exception ex)
{
}
}
For above iOS 11 you use the DidPickDocumentUrls since multipick is supported there :
private void DocPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs e)
{
try
{
List<NSUrl> filePath = e.Urls.ToList().Select(y => y.AbsoluteUrl).ToList();
//returns the list of images selected
}
catch (Exception ex)
{
AppLogger.LogException(ex);
}
}

Background Task is not registered and the code is not working when the application is in background

I'm using parse for sending push notification and its working perfectly but, in the background i.e., when the application is not running then the code for registering the background service is executed but, no task is set and the background task is not executed after receiving the notification. Please help!!
Here is the code for registering the background task
Debug.WriteLine("Registering task");
var taskRegistered = false;
var exampleTaskName = "BackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == exampleTaskName)
{
taskRegistered = true;
task.Value.Unregister(true);
break;
}
}
await BackgroundExecutionManager.RequestAccessAsync();
if (!taskRegistered)
{
Debug.WriteLine("Registering task inside");
var builder = new BackgroundTaskBuilder();
builder.Name = exampleTaskName;
builder.TaskEntryPoint = "Tasks.BackgroundTask";
builder.Name = "PushNotification";
builder.SetTrigger(new Windows.ApplicationModel.Background.PushNotificationTrigger());
BackgroundTaskRegistration task = builder.Register();
await BackgroundExecutionManager.RequestAccessAsync();
}
Code that is executed in the background:
public async void Run(IBackgroundTaskInstance taskInstance)
{
Debug.WriteLine("1");
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
string content = notification.Content;
Debug.WriteLine("2");
_deferral.Complete();
}

Multiple and Repetitive AsyncTask in Android

I have two asynctask working with each other. I'm using them for creating Restaurant menu. First web service gets menu's titles from database. Second web service gets items of title from database. I get title data in my first asynctask and item data in my second asynctask.
For example, I have ten menu titles. There are eight items for each title. I execute first asynctask and get all of menu titles. I want to call second asynctask in first asynctask's onPostExecute for get this title's item and add TextView. I have to wait finished every second task for add item respectively.
In short, I need call first AsyncTask and wait finish it. Then send request to second AsyncTask in First AsyncTask. I have to wait every request to finish. How can I wait ?
Here is the my code.
First AsyncTask
public class BaslikDoldurAS extends AsyncTask<String,String[][],String[][]>{
int ParamID;
public BaslikDoldurAS(String ParamID){
this.ParamID=Integer.parseInt(ParamID);
}
#Override
protected String[][] doInBackground(String... params) {
BaslikDoldur(ParamID);
return sonuc;
}
protected void onPostExecute(String[][] sonuc){
for(int i=0;i<baslikCount;i++){
MenuDoldurAS kontrol = new MenuDoldurAS(firma_id,sonuc[2][i]);
kontrol.execute();
}
}
}
my function which is used in first asyncTask
private String[][] BaslikDoldur(Integer ParamID){
PropertyInfo id = new PropertyInfo();
id.name= "id";
id.setValue(ParamID);
id.type = PropertyInfo.INTEGER_CLASS;
SoapObject request = new SoapObject(NAMESPACE, "BaslikDoldur");
request.addProperty(id);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(MenuURL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call("http://tempuri.org/BaslikDoldur", envelope);
SoapObject response = (SoapObject) envelope.getResponse();
sonuc[2]=new String[response.getPropertyCount()]; //baslik
baslikCount=response.getPropertyCount();
for(int i=0;i<response.getPropertyCount();i++){
Object property = response.getProperty(i);
if(property instanceof SoapObject){
SoapObject menu = (SoapObject) property;
sonuc[2][i] = menu.getProperty("menu_baslik").toString();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return sonuc;
}
Second AsyncTask
public class MenuDoldurAS extends AsyncTask<String,String[][],String[][]>{
int ParamID;
String Baslik;
public MenuDoldurAS(String ParamID,String Baslik){
this.ParamID=Integer.parseInt(ParamID);
this.Baslik=Baslik;
}
#Override
protected String[][] doInBackground(String... params) {
MenuDoldur(ParamID,Baslik);
return sonuc;
}
protected void onPostExecute(String[][] sonuc){
for(int i=0;i<count;i++){
String baslik="";
if(!baslik.equals(sonuc[2][i])){
baslik=sonuc[2][i];
TextView basliktxt = new TextView(Urun.this);
basliktxt.setText(sonuc[2][i]);
basliktxt.setTextSize(20);
basliktxt.setTextColor(Color.RED);
basliktxt.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
urunLayout.addView(basliktxt);
}
else{
TextView aciklamatxt = new TextView(Urun.this);
aciklamatxt.setText(sonuc[3][i]);
aciklamatxt.setTextColor(Color.parseColor("#0c0c7c"));
aciklamatxt.setTextSize(17);
aciklamatxt.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
urunLayout.addView(aciklamatxt);
}
}
}
}
my function which is used in second asyncTask
private String[][] MenuDoldur(Integer ParamID,String Baslik){
PropertyInfo id = new PropertyInfo();
id.name= "id";
id.setValue(ParamID);
id.type = PropertyInfo.INTEGER_CLASS;
PropertyInfo baslik = new PropertyInfo();
baslik.name= "baslik";
baslik.setValue(Baslik);
baslik.type = PropertyInfo.STRING_CLASS;
SoapObject request = new SoapObject(NAMESPACE, "MenuDoldur");
request.addProperty(id);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(MenuURL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call("http://tempuri.org/MenuDoldur", envelope);
SoapObject response = (SoapObject) envelope.getResponse();
sonuc[3]=new String[response.getPropertyCount()]; //aciklama ve fiyat
count = response.getPropertyCount();
for(int i=0;i<response.getPropertyCount();i++){
Object property = response.getProperty(i);
if(property instanceof SoapObject){
SoapObject menu = (SoapObject) property;
sonuc[3][i] = menu.getProperty("menu_aciklama").toString() + " - " + menu.getProperty("menu_fiyat").toString();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return sonuc;
}
If you want to wait until all AsyncTasks are done before proceeding, why don't you just put all of you work in doInBackground of the first AsyncTask?
Or you don't want to do this because you want to run the 10 "second tasks" in parallel? (Which, incidentally you're not doing anyway, because you're not using the THREAD_POOL Executor for your tasks.) If this is the case then why not just do something like
// variable accessible to both tasks
ArrayList<AsyncTask> mRunningTasks = new ArrayList<AsyncTask>();
// AsyncTask1
protected void onPostExecute(String[][] sonuc){
for(int i=0;i<baslikCount;i++){
MenuDoldurAS kontrol = new MenuDoldurAS(firma_id,sonuc[2][i]);
mRunningTasks.add(kontrol);
}
for (AsyncTask task : mRunningTasks) {
task.execute();
}
}
// AsyncTask2
protected void onPostExecute(...) {
boolean allComplete = true;
for (AsyncTask task : mRunningTasks) {
if (!task.getStatus().equals(AsyncTask.Status.FINISHED)) {
allComplete = false;
break;
}
}
if (allComplete) {
//do whatever
mRunningTasks.clear();
}
}

What is the TPL equivalent of rx's Observable.FromEventPattern?

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();
}
}

Resources