|
(1) does this block on the first item's await or will await on all?
(2) if the latter, what's the implication if the wrapped http client in
GetItemChildAsync is a singleton?
foreach (var i in items)
{
await GetItemChildAsync(i.Id);
}
|
|
|
|
|
1) Technically, the whole point of await is that it doesn't "block". It simply creates a continuation callback representing the rest of the method, and passes that to the task being awaited.
However, from a logical perspective, the rest of the method is "blocked" until the task returned from GetItemChildAsync completes. That happens when the task is awaited, so it "blocks" on the first item.
If you wanted to start all of the tasks at the same time, and then wait for them all to complete, you would use the Task.WhenAll method[^]:
IEnumerable<Task> tasks = items.Select(i => GetItemChildAsync(i.Id));
await Task.WhenAll(tasks);
2) None whatsoever. The HttpClient class[^] is safe to call from multiple threads at the same time. In fact, a singleton used to be the recommended way to use this class[^]. However, the current recommendation is to use the IHttpClientFactory service[^] to create an HttpClient instance on demand.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I want to convert dates come from database. I have the following API action:
[HttpGet]
public async Task<IActionResult> GetSpecialPostsList()
{
var posts = await myContext.Posts.Where(x => x.IsSpecialPost == true && x.IsActive == true).ToListAsync();
if (posts != null)
{
var revisedPost = new RevisedPost();
var revisedPostList = new List<RevisedPost>();
foreach (var post in posts)
{
revisedPost.CreationDate = DateConverter.GregToPersianDate(post.CreationDate.ToShortDateString());
revisedPost.Description = post.Description;
revisedPost.ImageFileName = post.ImageFileName;
revisedPost.IsActive = post.IsActive;
revisedPost.IsMainPost = post.IsMainPost;
revisedPost.IsSpecialPost = post.IsSpecialPost;
revisedPost.Title = post.Title;
revisedPost.Id = post.Id;
revisedPostList.Add(revisedPost);
}
return Ok(revisedPostList);
}
return NotFound("Data not found!");
}
The problem is that when new data is added to the list, all the items in the list are updated to the latest item values. This means all the items in the list are the same.
What is wrong with my code?
|
|
|
|
|
You keep updating and adding the "same" revisedPost object in your for loop (instead of creating a new instance). "Adding" does not make "copies" of "reference" (i.e. class) objects.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Thanks. I put the following code inside the loop and the problem solved.
var revisedPost = new RevisedPost();
|
|
|
|
|
In your send line, "posts" will also be non-null, so that if() will always be true. It should be if(posts.Any())
Truth,
James
|
|
|
|
|
I need to be able to send text to a selected worksheet that is already opened.
This, I think is close but it didn't work:
<pre lang="C#">
string wb = cmb_BookName.Text.ToString();
string ws = cmb_SheetName.Text.ToString();
if (chkContainer.Checked)
{
Excel.Application oexcel = new Excel.Application();
Excel.Workbook wkbk = (Excel.Workbook)oexcel.Workbooks[wb];
Excel.Worksheet wksk = (Excel.Worksheet)wkbk.Sheets[ws];
Range cellRange = wksk.Range["D48:D48"];
cellRange.Value = cboContainer.Text;
}
</pre>
The code builds without errors but when running it stops at the line trying to get the workbook and gives me an Exception Unhandled message.
So basically my question still is how do I work with an excel workbook that is already opened?
Seems like most of the articles that I find are opening an excel file and then working with it. In my case, I need to work with an already opened excel workbook.
|
|
|
|
|
You can't, unless it is a shared workbook. Excel locks files when you open them, to prevent changes affecting what you are doing.
Have a look here: c# - Is posible edit opened excel file - Stack Overflow[^]
But to be honest, if you need multiuser access to a file, you'd be a lot better off moving to a SQL Server / MySQL database as they are designed for that! Excel is a spreadsheet - and a damn good one - but it is not a DB, and shouldn't be used for one for anything beyond the trivial.
"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!
|
|
|
|
|
If the workbook is already opened in the same user session, you need to use Marshal.GetActiveObject[^] to get the running Excel instance, rather than starting a new instance.
Excel.Application oexcel = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
if (oexcel is null) throw new InvalidOperationException("Excel is not running.");
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Interopping through Excel.
In general, open files cannot be written to. This interop is a cheat, but with a big bottleneck and won't scale.
You might need an actual developer who can design a database, and those are expensive
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I'll venture this: msdocs[^] ... since noone has yet made a similar citation.
|
|
|
|
|
RedDk wrote: since noone has yet made a similar citation
Who is "noone"?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
Any malenky malchick az dim a citizen such as yourself might encounter at the mestos bar. I suppose.
modified 21-May-22 14:03pm.
|
|
|
|
|
Hello everybody, I pretty new to programming, what I want to do is a C# program that will send a message to a Microsoft teams group via a webhook connector of this group.
I was able to achieve it with a Desktop code(with form),but I need it to be a console program, I have pretty much copy/paste the code from my Desktop project to my console project, but there something that don't work. Here is my code from my desktop project:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test_team_2
{
public partial class Form1 : Form
{
string[] cmdLineMe = Environment.GetCommandLineArgs();
string webhookPdfTermine = "uri to my team groups;
HttpClient client = new HttpClient();
Message body = new Message();
string finalMessage = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < cmdLineMe.Length; i++)
{
finalMessage = cmdLineMe[i];
}
sendMsg();
}
public class Message
{
public string text { get; set; }
}
private async Task SendMsgAsync()
{
MessageBox.Show(finalMessage);
client.BaseAddress = new Uri(webhookPdfTermine);
body.text = finalMessage;
string serializeJson = JsonSerializer.Serialize(body);
StringContent content = new StringContent(serializeJson, Encoding.UTF8, "application/json");
_ = await client.PostAsync(client.BaseAddress, content);
}
void sendMsg()
{
_ = SendMsgAsync();
}
}
}
And here is my code for my console project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
namespace TeamBots
{
class Program
{
string[] cmdLineMe = Environment.GetCommandLineArgs();
string webhookPdfTermine = "uri to my team groups";
HttpClient client = new HttpClient();
Message body = new Message();
string finalMessage = "";
static void Main(string[] args)
{
Program myprog = new Program();
for (int i = 0; i < myprog.cmdLineMe.Length; i++)
{
myprog.finalMessage = myprog.cmdLineMe[i];
}
myprog.sendMsg();
}
private async Task SendMsgAsync()
{
Console.WriteLine(finalMessage);
client.BaseAddress = new Uri(webhookPdfTermine);
body.text = finalMessage;
string serializeJson = JsonSerializer.Serialize(body);
StringContent content = new StringContent(serializeJson, Encoding.UTF8, "application/json");
_ = await client.PostAsync(client.BaseAddress, content);
}
void sendMsg()
{
_ = SendMsgAsync();
}
public class Message
{
public string text { get; set; }
}
}
}
Like I said before, I pretty new to programming, the code was taken from website there and there...
If anyone can help it will be very appreciated.
Thank you
modified 18-May-22 15:09pm.
|
|
|
|
|
The first suspicious thing I see is you call an async method SendMsgAsync() without waiting for it. This means your Main method can execute the code following myprog.sendMsg() before the message is put on the network.
No problem you might think: There is no code there.... correct. And what happens to a console application when there is no more code to run. Well... it terminates the process (a bit simplified - you CAN get it to hang around, but you don't do that here, nor should you).
So my guess (without debugging which is how you REALLY find out these things) is that the program simply terminates before it can send any message.
You should make your Main async as well and await the async calls - then it will not terminate before completion (and you will also see any errors). You might also when googling find advise to call .Result or .Wait. Those are excellent advise for people who like to debug why there software occasionally hangs after they make a completely unrelated change. Sure you can use them if you know how it really works under the hood - but that is not a good topic for a beginner.
|
|
|
|
|
Thank you so much for your answer..
lmoelleb wrote: The first suspicious thing I see is you call an async method SendMsgAsync() without waiting for it. This means your Main method can execute the code following myprog.sendMsg() before the message is put on the network.
You was right, I was able to make it run by putting a Thread.Sleep(5000) after myprog.sendMsg(),is not optimal but it work
for now.
lmoelleb wrote: You should make your Main async as well and await the async calls - then it will not terminate before completion (and you will also see any errors). You might also when googling find advise to call .Result or .Wait. Those are excellent advise for people who like to debug why there software occasionally hangs after they make a completely unrelated change. Sure you can use them if you know how it really works under the hood - but that is not a good topic for a beginner.
I gonna check this.
Thank you so much for your help!
|
|
|
|
|
We have a system that uses Wonderware AOS objects with quite a bit of AOS scripting which interacts with a Sql Server database.
The scripting exists to access data from PLC's, add additional information from a Sql Server database, then write the combined data to a Sql Server table.
I'm wondering if anyone has converted this to a .net app (prefer C#), that would interrogates the PLC's with OPC, and performs the same functionality as above (as regards Sql Server).
The desired result is to stop using AOS objects and exit from Mobileframe.
Thanks.
|
|
|
|
|
I doubt it.
First, that's commercial software, innit? Means the source is not available for "porting".
Next problem, C# isn't used to interact with hardware directly. PLC-land is hardware interaction, and the GC of .NET means any .NET app execution can be put on hold by the system.
There's some OPC libraries available. Knock yourself out.
Bruce Armstrong 2022 wrote: The desired result is to stop using AOS objects Expensive?
If there was a free alternative, then this Wonderware would stop investing in it.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I have a data table with around 13000 rows. I want to add all of them to the database. Which method has the best performance, using C# and EF Core or using SQL Server stored procedures?
|
|
|
|
|
You're neglecting the specifics; e.g. SQL Server / EF Bulk Insert.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
|
Hi, i am using HZH_Controls.Controls.UCTextBoxEx to embed a UCTextBoxEx into a DataGridView control's TextBox field. The program is like below:
private HZH_Controls.Controls.UCTextBoxEx uctextBox1=null;
uctextBox1 = new HZH_Controls.Controls.UCTextBoxEx();
this.uctextBox1.Leave += new EventHandler(uctextBox1_Leave);
this.uctextBox1.TextChanged += new EventHandler(uctextBox1_TextChanged);
this.uctextBox1.Visible = false;
this.uctextBox1.KeyBoardType = HZH_Controls.Controls.KeyBoardType.UCKeyBorderNum;
private void uctextBox1_Leave(object sender, EventArgs e)
{
this.uctextBox1.Visible = false;
}
private void uctextBox1_TextChanged(object sender, EventArgs e)
{
this.dataGridViewAxisDefine.CurrentCell.Value = ((HZH_Controls.Controls.UCTextBoxEx)sender).Text;
this.uctextBox1.Visible = false;
}
private void DataGridViewAxisDefine_CurrentCellChanged(object sender, EventArgs e)
{
if ( this.dataGridViewAxisDefine.CurrentCell.ColumnIndex == 2)
{
Rectangle rectangle = dataGridViewAxisDefine.GetCellDisplayRectangle(dataGridViewAxisDefine.CurrentCell.ColumnIndex, dataGridViewAxisDefine.CurrentCell.RowIndex, false);
string value = dataGridViewAxisDefine.CurrentCell.Value.ToString();
this.uctextBox1.Text = value;
this.uctextBox1.Left = rectangle.Left;
this.uctextBox1.Top = rectangle.Top;
this.uctextBox1.Width = rectangle.Width;
this.uctextBox1.Height = rectangle.Height;
this.uctextBox1.Visible = true;
}
else
{
this.uctextBox1.Visible = false;
}
}
i just want people to click the TextBox field that is on the DataGridView to display a soft number pad and edit the numbers on it.
Now the error apears onto the line "
this.dataGridViewAxisDefine.CurrentCell.Value = ((HZH_Controls.Controls.UCTextBoxEx)sender).Text; ", saying "'
HZH_Controls.Controls.TextBoxEx ' object cannot cast to '
HZH_Controls.Controls.UCTextBoxEx ' "
The TextBoxEx is also a control of
HZH_Controls.Controls
, but i never use it.
The error message is translated from Chinese and may not precisely decribe the original meaning since i am using a Chinese version VS 2017.
So, my question is what's the problem cause this error and how to solve this problem? Many thanks in advance!
|
|
|
|
|
There is some code that you are not showing, but at a guess the sender variable is an instance of HZH_Controls.Controls.TextBoxEx which is not compatible with HZH_Controls.Controls.UCTextBoxEx . You need to check the documentation, or ask for help from, the people who write the HZH_Controls library.
|
|
|
|
|
When i change the "HZH_Controls.Controls.UCTextBoxEx" to "HZH_Controls.Controls.TextBoxEx", the program still works fine. The soft number pad can still pass numbers onto the Textbox field.
Yes, i need to check the documentation of HZH_Controls library.
Anyway thank you very much for your kind help!!!
|
|
|
|