Await for Task in C# not returing - c#

I am having an issue with the below code. In Debug it runs to the }); part after return true but never returns to the calling method. The below methods should connect to a zebra printer, send the ZLP code print and check the status after to ensure printing was successful. Then return true or false (will add exception handling after) and if true commit to the database the values printed.
private Boolean printLabel(string productName, double productWeight, String customerLabelType, String scaleNumber, String packageDate, String lotNumber, String barCode)
{
string s = dao.getCustomerLabel(customerLabelType);
s = s.Replace("<<<product_weight>>>", productWeight.ToString());
s = s.Replace("<<<product type>>>>", productName);
s = s.Replace("<<<barcode>>>", barCode);
s = s.Replace("<<<Packed Date:>>>", packageDate);
s = s.Replace("<<<Lot Number:>>>", lotNumber);
// RawPrinterHelper.SendStringToPrinter("ZDesigner GX420d", s);
string ipAddress = dao.getPrinterIp(scaleNumber);
try
{
return PrintOneLabelTask(ipAddress, s).Result;
}
catch (Exception ex)
{
}
return false;
}
private async Task<Boolean> PrintOneLabelTask(string theIpAddress, String ZPL_STRING)
{
await Task.Run(() =>
{
// Instantiate connection for ZPL TCP port at given address
Connection thePrinterConn = new TcpConnection(theIpAddress, TcpConnection.DEFAULT_ZPL_TCP_PORT);
ZebraPrinter printer = PrintHelper.Connect(thePrinterConn, PrinterLanguage.ZPL);
PrintHelper.SetPageLanguage(printer);
if (PrintHelper.CheckStatus(printer))
{
PrintHelper.Print(printer, ZPL_STRING);
if (PrintHelper.CheckStatusAfter(printer))
{
Console.WriteLine($"Label Printed");
}
}
printer = PrintHelper.Disconnect(printer);
Console.WriteLine("Done Printing");
return true;
});
return false;
}

When you return from a lambda expression, you are exiting the lambda expression and returning control to PrintOneLabelTask. You are not returning from PrintOneLabelTask at all. So in your code, the PrintOneLabelTask method will always return false.
You need to capture the result and return that instead.
private async Task<Boolean> PrintOneLabelTask(string theIpAddress, String ZPL_STRING)
{
bool result = false;
await Task.Run(() =>
{
// Instantiate connection for ZPL TCP port at given address
Connection thePrinterConn = new TcpConnection(theIpAddress, TcpConnection.DEFAULT_ZPL_TCP_PORT);
ZebraPrinter printer = PrintHelper.Connect(thePrinterConn, PrinterLanguage.ZPL);
PrintHelper.SetPageLanguage(printer);
if (PrintHelper.CheckStatus(printer))
{
PrintHelper.Print(printer, ZPL_STRING);
if (PrintHelper.CheckStatusAfter(printer))
{
Console.WriteLine($"Label Printed");
}
}
printer = PrintHelper.Disconnect(printer);
Console.WriteLine("Done Printing");
result = true;
});
return result;
}
Note: There are probably some other problems in your code... e.g., I don't see any possible scenario where the method will return false.

You seem to be experiencing a deadlock caused by the SynchronizationContext of your framework.
When you access Result of the Task<bool> returned from PrintOneLabelTask, you are blocking the current thread until that task is completed.
The Task<bool> is returned as soon as you hit await Task.Run(() =>: Task.Run returns another Task that will complete once the provided delegate has run to completion on the ThreadPool.
However, if your SynchronizationContext schedules continuations back onto the calling thread, it will attempt to resume PrintOneLabelTask on a thread that is already being blocked by Result.
There is your deadlock.
The solution
PrintOneLabelTask isn't really asynchronous at all; it is a synchronous method that is being run on the ThreadPool.
This isn't a problem as such, but it can lead to unmaintainable code with issues like the one you are currently experiencing.
The method should be written as the synchronous operation it is:
private void PrintOneLabel(string theIpAddress, string ZPL_STRING)
{
// Instantiate connection for ZPL TCP port at given address
Connection thePrinterConn = new TcpConnection(theIpAddress, TcpConnection.DEFAULT_ZPL_TCP_PORT);
ZebraPrinter printer = PrintHelper.Connect(thePrinterConn, PrinterLanguage.ZPL);
PrintHelper.SetPageLanguage(printer);
if (PrintHelper.CheckStatus(printer))
{
PrintHelper.Print(printer, ZPL_STRING);
if (PrintHelper.CheckStatusAfter(printer))
{
Console.WriteLine($"Label Printed");
}
}
printer = PrintHelper.Disconnect(printer);
Console.WriteLine("Done Printing");
}
(The method should also be void rather than bool because there is no situation where false could be returned).
Then call the method synchronously:
try
{
PrintOneLabel(ipAddress, s);
return true;
}
catch (Exception ex)
{
}
return false;
If you are building an app that has a UI thread, and this now causes your UI to freeze, then decide to offload to the ThreadPool at the highest level possible (usually an event handler):
public async void YourEventHandler(object sender, EventArgs e)
{
//...
bool result = await Task.Run(() => printLabel(//...
//...
}

Related

C# Check folder avoid blocking UI

This is my code in order to check if a directory exists. As the directory might be also a network folder, I put a timeout. The code works but it blocks my UI:
private delegate bool DirectoryExistsDelegate(string folder);
public static bool DirectoryExists(string path, int timeout = 2000)
{
bool retValue = false;
try
{
DirectoryExistsDelegate callback = new DirectoryExistsDelegate(Directory.Exists);
IAsyncResult result = callback.BeginInvoke(path, null, null);
if (result.AsyncWaitHandle.WaitOne(timeout, false))
return callback.EndInvoke(result);
return false;
}
catch
{
return false;
}
}
How to solve this?
Your method doesn't return until the Exists call is fully complete, so it will always block the UI. You've essentially started an asynchronous operation, but then sit around twiddling your thumbs waiting for it to complete.
As Dmitry Bychenko pointed out, you can do this in another thread and handle it very cleanly using async, await and Task.Run. To do this you should also make the event handler that's calling this method async, e.g.:
// An async event handler
public async void Button_Click(...)
{
...
var exists = await DirectoryExists(myPath);
...
}
public async Task<bool> DirectoryExists(string path)
{
return await Task.Run<bool>(() => Directory.Exists(path));
}

How to wait for HttpClient GetAsync call until it returns the request in C#

I am trying to get data by the HttpClient. Data vary in size, it could be from few bytes to megabyte. I noticed many times my application exist even before it returns from the GetAsync. How can I wait until GetAsync complete it call? From the main app:-
backup.DoSaveAsync();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Red;
// My app exist by printing this msg, wihout getting any data.
// someitmes it gets data and other times it gets notinng.
// I used sleep to wait to get the call completed.
Console.WriteLine("\nBackup has done successfully in SQL database")
public async void DoSaveAsync()
{
using (var client = GetHttpClient(BaseAddress, path, ApiKey))
{
Stream snapshot = await GetData(client, path);
if (snapshot != Stream.Null)
{
snapshot.Position = 0;
SaveSnapshot(snapshot);
}
}
}
private async Task<Stream> GetData(HttpClient client, string path)
{
HttpResponseMessage response = null;
try
{
response = await client.GetAsync(path);
System.Threading.Thread.Sleep(5000);
if (response.IsSuccessStatusCode == false)
{
Console.WriteLine($"Failed to get snapshot");
return Stream.Null;
}
return await response.Content.ReadAsStreamAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return Stream.Null;
}
}
Code update after the comments and answer:
// in my main app, I have this code.
// How can I get the completed task or any error return by the task here.
backup.DoBackupAsync().Wait();
public async Task<Stream> DoSaveAsync()
{
using (var client = GetHttpClient(BaseAddress, SnapshotPath, ApiKey))
{
try
{
Stream snapshot = await GetSnapshot(client, SnapshotPath);
if (snapshot != Stream.Null)
{
snapshot.Position = 0;
SaveSnapshot(snapshot);
}
return snapshot;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
As the method is async, the backup.DoSaveAsync() line only start a Task but doesn't wait for the result, so you may call Console.ReadLine (and possibly exit your program) before the task is completed. You should return Task instead of void - it's generally bad design to have a void async method, and younhave to await backup.DoSaveAsync() either via await (if you call from an async method), either via .Wait().
Also, in case of error in GetData, you don't return any error for DoSaveAsync - you may want to deal with this, in the current code, you would print "Failed to get snapshot" and then "Backup has done successfully in SQL database". Consider to not use Console.ReadLine in GetData and return a Task in DoSaveAsync indicating success
No need to put a thread.sleep here - you already await the result.

How to make a pause in a procedure and then return value after it?

I'm working on a C# project, want to make a small pause about 2 seconds inside a procedure.
Actually I have tried to use Invoke, but as you know, we can't use it inside a class this kind of procedure.
Here is my code for more details:
public class GenerateFile
{
public CSPFEnumration.ProcedureResult GenerateFaxFile(string Daftar_No, string Channelno, string NationalCode)
{
string script = string.Format(" DECLARE #RC INT " +
" DECLARE #Daftar_No INT = '{0}' " +
" DECLARE #hokm_type_code INT = 100 " +
" DECLARE #Channelno INT = '{1}' " +
" DECLARE #Id_No BIGINT = '{2}' " +
" EXEC #rc = [dbo].[Hokm_with_type] #Daftar_No, #hokm_type_code, #Channelno, #Id_No ",
Daftar_No,
Channelno,
NationalCode);
try
{
IEnumerable<string> commandStrings = Regex.Split(script, #"^\s*GO\s*$",
RegexOptions.Multiline | RegexOptions.IgnoreCase);
Connect();
foreach (string commandString in commandStrings)
{
if (commandString.Trim() != "")
{
using (var command = new SqlCommand(commandString, Connection))
{
command.ExecuteNonQuery();
}
}
}
DisConnect();
string FaxFilePath = InternalConstant.FaxFilePath + "\\" + string.Format("Lhokm{0}.tif", Channelno);
// I want to make a pause in here without locking UI
if (File.Exists(FaxFilePath))
return CSPFEnumration.ProcedureResult.Success;
else
return CSPFEnumration.ProcedureResult.Error;
}
catch (Exception ex)
{
InternalDatabase.GetInstance.InsertToPensionOrganizationException(ex);
return CSPFEnumration.ProcedureResult.Error;
}
}
}
I have tried await too, but I cant return a proper value. because in this procedure if I use await, the value will return before finishing await.
Edit:
And also I dont want to use Thread.Sleep because it will lock UI.
Thanks for any helping.
Use async await feature :
Mark your method as async .
Add Task.Delay(2000) as the waited task.
public async CSPFEnumration.ProcedureResult GenerateFaxFile(string Daftar_No, string Channelno, string NationalCode)
{
-----
// I want to make a pause in here without locking UI
await Task.Delay(2000);
-----
}
Asking for downvotes:
DoEvents
Warning: Total, Complete and Inexcusably Flagrant Barnyard Programming:
// before call (disable the UI element that called this so it can't re-enter)
DateTime st = DateTime.Now();
while(DateTime.Now.Subtract(st).TotalSeconds<3)
System.Windows.Forms.DoEvents();
// after call (re-enable UI element)
This will appear to work. No responsibility if people point and laugh.
Hey, you asked!
You can look around Task.Delay() it will not block current thread and continue execution after number of milliseconds.
Exmaple usage from msdn:
Stopwatch sw = Stopwatch.StartNew();
var delay = Task.Delay(1000).ContinueWith(_ =>
{ sw.Stop();
return sw.ElapsedMilliseconds; } );
Console.WriteLine("Elapsed milliseconds: {0}", delay.Result);
// The example displays output like the following:
// Elapsed milliseconds: 1013
Or maybe look around Timer class.
I can see it working with events or Tasks (if you cannot use async / await). This is how to create event. We can use separate Thread to check if file is created and fire event if it is:
public class FileGenEventArgs : EventArgs
{
public string ProcedureResult { get; set; }
}
public class GenerateFile
{
public event EventHandler<FileGenEventArgs > fileCreated;
public GenerateFile()
{
// subscribe for this event somewhere in your code.
fileCreated += GenerateFile_fileCreated;
}
void GenerateFile_fileCreated(object sender, FileGenEventArgs args)
{
// .. do something with args.ProcedureResult
}
private void FileCheck()
{
Thread.Sleep(2000); // delay
fileCreated(this, new FileGenEventArgs()
{
ProcedureResult = File.Exists(FaxFilePath) ?
CSPFEnumration.ProcedureResult.Success :
CSPFEnumration.ProcedureResult.Error
});
}
public void GenerateFaxFile(string Daftar_No, string Channelno, string NationalCode)
{
try
{
// this .Sleep() represents your sql operation so change it
Thread.Sleep(1000);
new Thread(FileCheck).Start();
}
catch (Exception ex)
{
InternalDatabase.GetInstance.InsertToPensionOrganizationException(ex);
}
}
}
Pros :
Pause that you wanted
Doesn't block the UI thread.
Event-based approach (which is proper way of dealing with this kind of problems)
Cons :
Requires to refactor your code
The most easy thing to wait while keeping the UI responsive is using async-await.
To do this, you must declare your function async, and return Task instead of void and Task<TResult> instead of TResult:
public async Task<CSPFEnumration.ProcedureResult> GenerateFaxFile(
string Daftar_No,
string Channelno,
string NationalCode)
{
// do your stuff,
}
Now whenever you do something that takes some time, use the async version of the function to start the process. While this process is running, you can do other stuff. When you need the result await for the task, and you get the void if the async returns Task, or the TResult if the async returns Task<TResult>
public async Task<CSPFEnumration.ProcedureResult> GenerateFaxFile(
string Daftar_No,
string Channelno,
string NationalCode)
{
IEnumerable<string> commandStrings = Regex.Split(
script, #"^\s*GO\s*$", RegexOptions.Multiline | RegexOptions.IgnoreCase);
Connect();
foreach (var commandString in commandStrings)
{
if (commandString.Trim() != "")
{
using (var command = new SqlCommand(commandString, Connection))
{
Task<int> task = command.ExecuteNonQueryAsync();
// while the command is being executed
// you can do other things.
// when you need the result: await
int result = await task;
// if useful: interpret result;
}
}
}
DisConnect();
... etc.
}
Every function that calls an async function should be declared async
every async function returns Task instead of void and Task<TResult> instead of TResult
There is only one exception: the event handler may return void.
Example of async event handler:
private async void OnButton1_Clicked(object sender, ...)
{
var task = GenerateFaxFile(...);
// while the fax file is generated do some other stuff
// when you need the result:
var procedureResult = await task;
Process(procedureResult);
}
Note that everything is processed by the UI thread. The only difference is that as soon as anything time consuming happens, the process doesn't have a busy wait, but processes UI input.
The above is enough to keep your UI responsive. You said you wanted to know how to wait some time. From the rest of your question I understand that you meant: how to interrupt the procedure while it is waiting for something, so the UI can do other thing. If you really need to wait some time while keeping the UI responsive, use Task.Delay(TimeSpan).
Eric Lippert (thanx Eric!) explained async-await as follows in Stackoverflow - async/await - Is this understanding correct?
Suppose for breakfast you have to toast bread and cook eggs. There are several scenarios for it:
Start toasting bread. Wait until it is finished. Start cooking eggs, wait until it is finished. Synchronous processing. While you are waiting for the bread to toast you can't do anything else.
Start toasting bread, while the bread is being toasted start cooking eggs. when the eggs are cooked wait until the bread finished toasting. This is called Asynchronous, but not concurrent. It is done by the main thread and as long as this thread does something, the main thread can't do anything else. But while it is waiting it has time to do other things (make some tea for instance)
Hire cooks to toast the bread and cook the eggs. Wait until both are finished. Asynchronous and concurrent: the work is done by different threads. This is the most expensive because you have to start new threads.
Finally a note about your exception handling
Do you notice that if an exception occurs you don't disconnect?. The proper way to make sure that disconnect is always called is the following:
try
{
Connect();
... do other stuff
}
catch (Exception exc)
{
... process exception
}
finally
{
Disconnect();
}
The finally part is always executed, regardless of any exception being thrown or not.
You can use simple Thread Pool to archive this. However your return has to do asynchronously so it doesn't lockup the gui.
public void GenerateFaxFile(string Daftar_No, string Channelno,
string NationalCode, Action<CSPFEnumration.ProcedureResult> result)
{
ThreadPool.QueueUserWorkItem(o =>
{
string script = "your script";
try
{
// more of your script
// I want to make a pause in here without locking UI
while (true)
{
// do your check here to unpause
if (stopMe == true)
{
break;
}
Thread.Sleep(500);
}
if (File.Exists(FaxFilePath))
{
result(CSPFEnumration.ProcedureResult.Success);
return;
}
else
{
result(CSPFEnumration.ProcedureResult.Error);
}
}
catch (Exception ex)
{
InternalDatabase.GetInstance.InsertToPensionOrganizationException(ex);
result(CSPFEnumration.ProcedureResult.Error);
return;
}
});
}
public void HowToUseMe()
{
GenerateFaxFile("", "", "", result => {
if (result == CSPFEnumration.ProcedureResult.Error)
{
// no good
}
else
{
// bonus time
}
});
}
You should use the old good background thread (see answer written by FabJan) or you can use async and await with synchronization context:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void buttonStart_Click(object sender, EventArgs e)
{
await progressBar1.DoProgress(2000);
Trace.WriteLine("Done");
MessageBox.Show("Done");
}
private void buttonMoveButton1_Click(object sender, EventArgs e)
{
//to prove UI click several times buttonMove while the task is ruunning
buttonStart.Top += 10;
}
}
public static class WaitExtensions
{
public static async Task DoProgress(this ProgressBar progressBar, int sleepTimeMiliseconds)
{
int sleepInterval = 50;
int progressSteps = sleepTimeMiliseconds / sleepInterval; //every 50ms feedback
progressBar.Maximum = progressSteps;
SynchronizationContext synchronizationContext = SynchronizationContext.Current;
await Task.Run(() =>
{
synchronizationContext.OperationStarted();
for (int i = 0; i <= progressSteps; i++)
{
Thread.Sleep(sleepInterval);
synchronizationContext.Post(new SendOrPostCallback(o =>
{
Trace.WriteLine((int)o + "%");
progressBar.Value = (int)o;
}), i);
}
synchronizationContext.OperationCompleted();
});
}
}
It could appear that MessageBox done Shows before the ProgressBar is on its Maximum. I blame for this magic animation of progressBar in Windows 8. Please correct me if I am wrong.

How can I convert a function with a ref paramter to an async function?

I have the following function which I want to convert to an async / non locking function.
Here is the function in its currenc form:
private static void BlockForResponse(ref bool localFlag)
{
int count = 0;
while (!localFlag)
{
Thread.Sleep(200);
if (count++ > 50) // 200 * 50 = 10 seconds
{
//timeout
throw new TimeOutException();
}
}
}
here is my attempt:
private static async Task BlockForResponse(ref bool localFlag)
{
int count = 0;
while (!localFlag)
{
await Task.Delay(200);
if (count++ > 50) // 200 * 50 = 10 seconds
{
//timeout
throw new TimeOutException();
}
}
}
however I get a compile error saying that async functions cant have ref or out parameters. However this is the core functionality of the function.
Is it possible to convert it to an async function?
Explanation of code:
I must admit this is an odd piece of code, let me try an explain what its trying to do:
so there is a 3rd party dll which I need to use. Which provides me with services, I sadly have no control over this dll.
The way it works,
I call a command in the dll providing it a callback function which it calls once it has finished the task.
I can only move on to what I want to do once I have the result from that call. hence the need fro this function.
I make the call to the dll, providing it with a call back function:
private bool _commandFlag = false;
private bool _commandResponse;
public async Task ExecuteCommand(string userId, string deviceId)
{
var link = await LinkProviderAsync.GetDeviceLinkAsync(deviceId, userId);
try
{
//execute command
if (link.Command(Commands.ConnectToDevice, CallBackFunction))
{
BlockForResponse(ref _commandFlag);
return; //Received a response
}
else
{ //Timeout Error
throw new ConnectionErrorException();
}
}
catch (Exception e)
{
throw e;
}
}
private void CallBackFunction(bool result)
{
_commandResponse = result;
_commandFlag = true;
}
The way it works, I call a command in the dll providing it a callback function which it calls once it has finished the task.
Then what you really want is to use TaskCompletionSource<T> to create a TAP method, something similar to this.
public static Task<bool> CommandAsync(this Link link, Commands command)
{
var tcs = new TaskCompletionSource<bool>();
if (!link.Command(command, result => tcs.TrySetResult(result)))
tcs.TrySetException(new ConnectionErrorException());
return tcs.Task;
}
With this extension method in place, your calling code is much cleaner:
public async Task ExecuteCommand(string userId, string deviceId)
{
var link = await LinkProviderAsync.GetDeviceLinkAsync(deviceId, userId);
var commandResponse = await link.CommandAsync(Commands.ConnectToDevice);
}
The problem with combining async and ref is that code inside an async function can run even after the method returns. So, if you did something like:
async Task BlockForResponseAsync(ref bool localFlag)
{
while (!localFlag)
{
...
}
}
void SomeMethod()
{
bool flag = false;
BlockForResponseAsync(ref flag); // note: no await here
}
Then the local variable flag would stop existing after SomeMethod() returned, but BlockForResponseAsync(), which has a reference to that variable, could still be executing. This is why the above code won't compile.
Basically, what you need is a closure, and in C#, ref doesn't create closures, but lambdas do. This means you can write your method like this:
async Task BlockForResponseAsync(Func<bool> localFlagFunc)
{
while (!localFlagFunc())
{
...
}
}
And use it like this:
bool flag = false;
var task = BlockForResponseAsync(() => flag);
// other code here
flag = true;
await task; // to make sure BlockForResponseAsync() completed successfully
This way also indicates your intention better. ref usually means something like: "give me a variable with some value, and I will change that value", which is not what you want here. On the other hand Func<T> means "give me something that I can use retrieve some value, potentially multiple times".

Can I get rid of this horrible blocking code?

I need some help, In a web api service call, I need to call a function in a dll that executes asynchronously, and then gives the response in a call back function. Now normally this would be good , but now with a web api the idea is to execute the command and then return the response.
Below is my current code which works, but I think its terrible code, its everything you don't want to do. Especially on a web server when this code is going to run for every request.
[HttpGet]
public HttpResponseMessage Off(string id)
{
APNLink.Link link = LinkProvider.getDeviceLink(id, User.Identity.Name);
if (link.LinkConnectionStatus == APNLink.ConnectionStatus.Connected)
{
link.RelayCommand(APNLink.RelayNumber.Relay1, APNLink.RelayCommand.OFF, test);
BlockForResponse();
var msg = Request.CreateResponse(HttpStatusCode.OK);
return msg;
}
else
{
if (link.Connect())
{
var status = link.LinkConnectionStatus;
int timeout = 0;
while (status != APNLink.ConnectionStatus.Connected)
{
Thread.Sleep(500);
status = link.LinkConnectionStatus;
if (status == APNLink.ConnectionStatus.Connected)
{
break;
}
if (timeout++ > 16)
{
var msg1 = Request.CreateResponse(HttpStatusCode.RequestTimeout);
return msg1;
}
}
link.RelayCommand(APNLink.RelayNumber.Relay1, APNLink.RelayCommand.OFF, test);
BlockForResponse();
var msg = Request.CreateResponse(HttpStatusCode.OK);
return msg;
}
else
{
var msg2 = Request.CreateResponse(HttpStatusCode.BadRequest);
return msg2;
}
}
}
bool flag = false;
public void test(bool var)
{
flag = true;
}
private static bool BlockForResponse()
{
int count = 0;
while (!flag)
{
Thread.Sleep(500);
if (count > 10)
{
//timeout
return false;
}
}
return true;
}
now the thing is I have to block when waiting for the dll, Connection to be connected, only then can I execute the command. once I have executed the command. I then have to block again for the response.
Another aspect is, Can I actually block on an asp.net thread? Surely each request is not handled on its own thread?
Is there any way I can make this code neater and most importantly more efficient?
Answering the question:
In a web api service call, I need to call a function in a dll that
executes asynchronously, and then gives the response in a call back
function.
IMO, the best way of doing this is to make your controller method asynchronous and use TaskCompletionSource to wrap the callback of your DLL. Some good additional reads:
Using Asynchronous Methods in ASP.NET MVC 4.
The Nature of TaskCompletionSource<TResult>.
The code may look something like this:
[HttpGet]
public async Task<HttpResponseMessage> Off(string id)
{
APNLink.Link link = LinkProvider.getDeviceLink(id, User.Identity.Name);
if (link.LinkConnectionStatus == APNLink.ConnectionStatus.Connected)
{
var tcs = new TaskCompletionSource<object>();
CallbackType test = delegate {
tcs.SetResult(null);
};
link.RelayCommand(
APNLink.RelayNumber.Relay1,
APNLink.RelayCommand.OFF,
test);
// BlockForResponse();
await tcs.Task; // non-blocking
var msg = Request.CreateResponse(HttpStatusCode.OK);
return msg;
}
// ...
}

Categories

Resources