|
Not really. Spinning up a new Task and awaiting it is only really useful if you want to move the processing onto a background thread. On the server, that doesn't make any sense.
On the server-side, you'd want to have async calls to the database. Entity Framework offers various async methods. Or, if you're using raw ADO.NET, use DbConnection.OpenAsync[^], DbCommand.ExecuteReaderAsync[^], etc.
On the client, you want the network call to be async , so that your code isn't blocked waiting for the network request to complete. If you're using HttpClient or RestClient , the default methods for calling the server will be async . If you're using WCF or HttpWebRequest , you might have to jump through a few hoops to make a nice async method.
You should generally avoid async void methods:
Avoid async void methods | You’ve Been Haacked[^]
Instead, follow David Fowler's advice[^] and store the Task returned from your async method in a discard variable:
private void SaveExecuted()
{
_ = SaveAsync();
}
private async Task SaveAsync()
{
try
{
IServerAPI serverAPI = new ServerAPI();
if (Company.Id == 0)
{
Company.Id = await serverAPI.AddCompanyAsync(Company);
}
else
{
await serverAPI.UpdateCompanyAsync(Company);
}
}
finally
{
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: Not really. Spinning up a new Task and awaiting it is only really useful if you want to move the processing onto a background thread. On the server, that doesn't make any sense.
OK, so what would the server methood look like? I'm a bit confused
In that Fowler article, he has this example:
public async Task<int> DoSomethingAsync()
{
var result = await CallDependencyAsync();
return result + 1;
}
I thought that the server method had to return a task to make it Async?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 29-Jan-21 18:12pm.
|
|
|
|
|
The server method can return a Task , if it's doing work that can be executed asynchronously. For example, talking to a database, calling another API, or accessing the file system.
Whether or not the server method returns a Task , the calling client can use a Task to call the server. The network request will be the asynchronous part of the call; the client doesn't care how the server handles the request.
Eg:
Server:
public int Foo()
{
return 42;
}
public async Task<int> BarAsync()
{
using (var connection = new SqlConnection("..."))
using (var command = new SqlCommand("...", connection))
{
await connection.OpenAsync();
object result = await command.ExecuteScalarAsync();
return Convert.ToInt32(result);
}
} Client:
private readonly HttpClient _client;
public async Task<int> FooAsync()
{
using (var response = await _client.GetAsync("/api/foo"))
{
response.EnsureSuccessStatusCode();
return await response.ReadAsAsync<int>();
}
}
public async Task<int> BarAsync()
{
using (var response = await _client.GetAsync("/api/bar"))
{
response.EnsureSuccessStatusCode();
return await response.ReadAsAsync<int>();
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks.
What I'm after here is to make my WPF app responsive during Web API calls. You provided some asynv/await examples, but what the don't show is.. what is it that makes
command.ExecuteScalarAsync()
asynchronous? Isn't ExecuteScalarAsync just returning a task? This is my real question... What is it on the server that allows the client to await it?
I'm using Linq-To-Sql on my server. Right now, my DAL code is pretty straightforward:
public CompanyEntity GetCompanyById(int companyId)
{
using (var dc = GetDataContext())
{
var cust = (from c in dc.Companies
where c.Id == companyId
select new CompanyEntity
{
Id = c.Id,
CompanyName = c.CompanyName
}).FirstOrDefault();
return cust;
}
}
Then, in my WPF ViewModel I do
public override void Load(int id)
{
Customer = AppCore.BizObject.GetCustomerById(id);
}
But to make it truly async, I would need
public override async void Load(int id)
{
await Task.Factory.StartNew(() =>
{
var customer = AppCore.BizObject.GetCustomerByIdAsync(id);
});
}
I'm using a task to keep the UI responsive. But what should the server side method look like? Something close to this??
public Task<CompanyEntity> GetCompanyByIdAsync(int companyId)
{
using (var dc = GetDataContext())
{
var cust = (from c in dc.Companies
where c.Id == companyId
select new CompanyEntity
{
Id = c.Id,
CompanyName = c.CompanyName
}).FirstOrDefault();
return cust;
}
}
Thanks again!
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Again, there are two parts to this.
On the server, ExecuteScalarAsync returns a Task , which uses an IO completion port behind the scenes to complete the task when the response has been received from the database. This allows the server to reuse the same thread-pool thread to service other requests whilst it waits for your database query to finish.
The client doesn't care what the server is doing. All it cares about is sending a request and receiving a response. But again, the network library can use an IO completion port behind the scenes so that your thread isn't tied up waiting for a response from the server. It doesn't matter to the client whether the server has implemented the API using Tasks , or using a synchronous method; the network communication is the target for the asynchronous code.
I think at least part of the confusion here stems from the fact that you're using the same interface for the server implementation of the API and the client code to call that API. It's perfectly valid to have an async method on the client calling a synchronous method on the server.
With Linq2Sql, it's not simple to make your server method async *. But that doesn't mean the client can't be async .
protected override void Load()
{
_ = LoadAsync();
}
private async Task LoadAsync()
{
var customer = await AppCore.BizObject.GetCustomerByIdAsync(id);
...
} Again, take note of the fact that you should avoid async void methods, and try to avoid spinning up a new Task just to push work off the UI thread.
* You'd have to manually convert the query to a SqlCommand and execute it by hand. Mike Taulty made a start on this back in 2007, before async /await was around:
LINQ to SQL: Asynchronously Executing Queries | Mike Taulty[^]
Perhaps try something like this:
public async Task<CompanyEntity> GetCompanyByIdAsync(int companyId)
{
using (var dc = GetDataContext())
{
var query = (from c in dc.Companies
where c.Id == companyId
select new CompanyEntity
{
Id = c.Id,
CompanyName = c.CompanyName
});
using (var command = (SqlCommand)dc.GetCommand(query))
using (var reader = await command.ExecuteReaderAsync())
{
return dc.Translate<CompanyEntity>(reader).FirstOrDefault();
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: It's perfectly valid to have an async method on the client calling a synchronous method on the server.
This is what I'm hung up on. To call a synchronous method on the server using async on the client, then the server method needs to return a Task, correct? Unless there's some other way?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
You can call any server method using async on the client. The client doesn't know or care how the server handles the call. So long as the request and response payloads match what is expected, it doesn't matter whether the server uses a synchronous method, an async method, or quantum fluctuations in the migration pattern of swallows.
From the client's perspective, the network call is the async part. If you're using HttpClient or RestSharp, the network calls will be async by default. If you're using WebClient or WebRequest , there are TAP[^] overloads available. The WCF wizard provides an option to generate TAP methods in the proxy, or you can build a Task from the BeginFoo /EndFoo pair.
The only time you'll have problems is if you're trying to use the same interface to define the client and the server API. But even then, you can still have a synchronous server API - just omit the async keyword, and use Task.FromResult to return the result.
public Task<int> Foo()
{
return Task.FromResult(42);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I have a list of members (integer numbers):
List<int> emptyRows = new List<int>();
for (int i = 0; i < LastRow+1; i++)
{
if (worksheet.Cells[i, 0].Value.IsEmpty == true)
{
emptyRows.Add(i);
}
}
I want to subtract 1 from all the members (member1 -1, member2 -1, ...).
How can I do this using Linq?
Please guide me by giving an example code.
Thanks.
modified 27-Jan-21 11:56am.
|
|
|
|
|
You can't: for the same reason that you can't use a foreach loop to do it.
Integers are value types: every time you try to use one, you create a copy, there is no reference to it.
If you use a class, you can do it using the ForEach method:
public class foo
{
public int val { get; set; }
}
List<foo> foos = new List<foo>();
for (int i = 0; i < 10; i++)
{
foos.Add(new foo() { val = i });
}
foos.ForEach(f => f.val += 10);
foreach (foo foo in foos)
{
Console.WriteLine(foo.val);
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I tried to ask a simplified mode of my problem. Let's explain it. I want to delete empty rows in DevExpress Spreadsheet by code. I tried this:
IWorkbook workbook = spreadsheetControl.Document;
Worksheet worksheet = workbook.Worksheets["Sheet1"];
CellRange range = worksheet.GetUsedRange();
int LastRow = range.BottomRowIndex;
List<int> emptyRows = new List<int>();
for (int i = 0; i < LastRow+1; i++)
{
if (worksheet.Cells[i, 0].Value.IsEmpty == true)
{
emptyRows.Add(i);
}
}
Search:
if (emptyRows.Count >= 1)
{
foreach (var item in emptyRows)
{
worksheet.Rows[item].Delete();
emptyRows.RemoveAt(item);
goto Update;
}
}
Update:
if (emptyRows.Count >= 1)
{
for (int i = 0; i < emptyRows.Count; i++)
{
emptyRows[i] = emptyRows[i]-1;
}
goto Search;
}
In the code above, I try to list empty rows. Then, I delete them based on the created list of Row Indexes. The problem is that each time a row is deleted, other empty row indexes are changed (row-1). I need to find a way to subtract each item in the list by 1 in each iteration. The second solution is to select empty rows and delete them at once. I couldn't use the second way too. Because, I don't know how to do multiple selection by code. Please help me.
|
|
|
|
|
Simple solution: don't go from the beginning to the end, deleted from the end to the beginning.
Run the loop backwards!
And do yourself a massive favour: stop using labels and goto. There is absolutely no need to use them in that code - have you not heard of break in a loop?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
foreach (var item in emptyRows.Reverse<int>())
{
worksheet.Rows[item].Delete();
}
I tried to use Linq namespace for reversing my list. It worked.
Now, I'm trying to use this method for deleting duplicates.
|
|
|
|
|
You don't need Linq to iterate a list in reverse:
for (int index = emptyRows.Count - 1; index >= 0; index--)
{
int item = emptyRows[index];
worksheet.Rows[item].Delete();
} Using Reverse will take longer and use more memory.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You cannot use "foreach" to do this. "Foreach" uses an iterator to navigate over the collection. Looking at the documentation for Iterator says you cannot add/remove items to the collection being iterated over else you invalidate the iterator.
You must use an indexer, "for", to remove items from the collection. The best way to do that is to start at the end of the array (collection) and move backwards toward zero.
|
|
|
|
|
Hello,
I have Windows Forms C# desktop app with docked window layout.
Ie. few document window and few tool windows.
In that case I used WeifenLuo.WinFormsUI.Docking library.
My question is what is best way to realize event handling after document content change.
Let's consider case.
We can change content in active document window and we can just change active document window. In both cases tool windows should be refreshed based on active document window content (item selected ie.)
|
|
|
|
|
Hi I have a linq query in the following way where two column names from 2 different tables is same, which is giving me error, can somebody please help me in this? How to resolve with alias names if the names are same from 2 different tables and they are used either in select or in group - thank you so much in advance.
Here is my linq query:
ViolationsUniquePenaltyApplied = (from a in violations
join b in programs on a.ViolationTypeId equals b.ViolationTypeId
join d in branches on b.BranchId equals d.BranchId
join e in programs on a.ViolationSubtypeId equals e.ViolationTypeId into es from e in es.DefaultIfEmpty()
where a.ApplyUniquePenalty == true
group new { a.ViolationSubtypeId, e.ViolationTypeName, a?.ViolationTypeId, b?.ViolationTypeName, b?.ParentViolationId, d?.BranchId, d?.Name, a.PenaltyAssessed, a.ViolationId, a.ViolationNumber }
by new { a?.ViolationTypeId, b?.ViolationTypeName, d?.BranchId, d?.Name, a.ViolationId }
into g
select new
{
ViolationTypeId = g.FirstOrDefault().ViolationTypeId,
ViolationType = g.FirstOrDefault().ViolationTypeName,
ParentViolationType = (g.FirstOrDefault()?.ParentViolationId != null) ? (from e in programs where g.FirstOrDefault()?.ViolationTypeId == e.ParentViolationId select e.ViolationTypeName)?.FirstOrDefault() : g.FirstOrDefault().ViolationTypeName,
BranchId = g.FirstOrDefault().BranchId,
Branch = g.FirstOrDefault().Name,
PenaltyAssessed = g.Sum(pc => pc.PenaltyAssessed),
Cnt = g.Count(),
ViolationNumber = g.FirstOrDefault().ViolationNumber,
CountOrId = (g.Count() == 1) ? g.Count() : g.FirstOrDefault().ViolationId,
PenaltyPerUnit = g.Sum(pc => pc.PenaltyAssessed) / g.Count()
})
|
|
|
|
|
What does the "simple" stand for?
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Assuming the problem is the two ViolationTypeName columns in your group statement - and assuming you actually need to group by both - you'll need to specify a different property name for one or both of them.
group new
{
a.ViolationSubtypeId,
e.ViolationTypeName,
a?.ViolationTypeId,
ViolationTypeName2 = b?.ViolationTypeName,
b?.ParentViolationId,
d?.BranchId,
d?.Name,
a.PenaltyAssessed,
a.ViolationId,
a.ViolationNumber
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 26-Jan-21 3:46am.
|
|
|
|
|
hi,
i like to build a restaurant billing system. i would be using Visual Studio 2019. language would be C#.
my question is regarding printing bills.
what is the easiest 3rd party software I can use with Visual Studio 2019 for printing bills to a thermal printer.
|
|
|
|
|
If you are using Windows, the issue is whether your thermal printer has a driver for Windows. If it does, you print to it like you would print to any other Windows printer.
Note that your code will still have to take certain details into account, such as the narrow width of a page, the length of the "page" (a POS printer typically uses a roll that is of "infinite" length). Any such details should be available in the technical reference manuals of the POS printer(s) that you intend to support, or on the manufacturers' websites.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Oops! I missed the button ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Just to add to what Daniel has said, start with the manufacturers site or tech support - most will have sample code that should give you a "starter".
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi,
I'm trying to use Devexpress library in my own project. I use PivotGridControl to load an excel data into PivotTable. For a test, I used the following code:
public Form1()
{
ExcelDataSource myExcelSource = new ExcelDataSource();
myExcelSource.FileName = @"C:\Users\Fardin\Desktop\Book1.xlsx";
ExcelWorksheetSettings worksheetSettings = new ExcelWorksheetSettings("Sheet1", "A1:B20");
myExcelSource.SourceOptions = new ExcelSourceOptions(worksheetSettings);
myExcelSource.SourceOptions.SkipEmptyRows = false;
myExcelSource.SourceOptions.UseFirstRowAsHeader = true;
myExcelSource.Fill();
pivotGridControl1.DataSource = myExcelSource;
}
But there is following error in
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I upload the whole project package:
https://www.dropbox.com/s/vmokcgu21jqohpo/DXApplication1.rar?dl=0
And for Excel file:
Dropbox - Book1.xlsx - Simplify your life[^]
Please guide me.
modified 22-Jan-21 5:08am.
|
|
|
|
|
[Devexpress Forums]=>
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Impossible to answer properly, since you didn't even tell us which line / variable is null .
At a guess, the pivotGridControl1 is initialized in the compiler-generated InitializeComponent method, which you haven't called. Therefore, the field is null , and you get a NullReferenceException on the pivotGridControl1.DataSource = myExcelSource; line.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|