Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
So I have some code that I need to execute when an form activates its Shown event, and I need to await that code. I am doing this:
form.Shown += async (sender, args) => await BufferOpen(CurrentPath, CurrentEncoding, 1024 * 1024 * 5, statusProgressForm.progressBar1);
statusProgressForm.Show();
But the code still continues without awaiting the BufferOpen method. How can I do this with an anonymous function?
EDIT:
Ok, so I think I screwed up the original post. Sorry about that. What I'm really trying to do is show a form and THEN on the shown event perform intensive code, as before when I just did this:
form.Show();
DoIntensiveTasks();
The GUI on the form would not properly load and the labels and such wouldn't display properly. I need to wait until the form is completely shown then do stuff.
The problem is that because you used an expression lambda it's only performing the one expression as a part of that anonymous method, and thus the Show call isn't a part of that anonymous method. All it takes is to use brackets to make it a statement lambda:
form.Shown += async (sender, args) =>
{
await BufferOpen(CurrentPath, CurrentEncoding, 1024 * 1024 * 5, statusProgressForm.progressBar1);
statusProgressForm.Show();
};
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So I was experimenting with Blazor and I'd like to understand the difference between these two.
<p #onclick="OnClickCallback">Click me normal</p>
<p #onclick="async () => await OnClickCallback()">Click me lambda</p>
#code {
private async Task OnClickCallback()
{
await Task.Delay(500);
}
}
What is the difference between these two approaches. What happens behind the scenes. I've found some issues with EF Core (which is a whole other topic) where the lambda approach did not throw an exception, and the normal did.
What is the difference between these two approaches
It adds another function, which means it introduces another call on the stack.
Also, because this is an async lambda, it allocates another Task instance.
This being said, using the former will have a negligible performance benefit.
For completeness, there is a third alternative that would prevent another Task being used, but would still result in another function call:
() => OnClickCallback()
In Blazor, that's a very good question. I don't really know if they compile differently, but I wanted to mention something that's quite important: a lambda method can pass a variable other than the normal event variables that go with the event. It's very useful to do something like this:
#foreach (var item in SomeCustomList)
{
<p #onclick="async (e) => await OnClickCallback(e, item )">Click me lambda</p>
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am working on some APIs that hit other APIs, testing them, and then storing results in a Postgres. I've already completed other sections of it with using EF and async/await so the following code block is essentially a copy/paste..
public async Task<OperationResponse> UpsertEvents(ILogger log, MCVPEvent eventToAdd, ICollection<MCVPEventStep> eventStepsToAdd)
{
int saveResult;
long saveResultId;
try
{
var eventDto = this.mapper.Map<MCVPEventDto>(eventToAdd);
eventDto.MCVPEventSteps = this.mapper.Map<ICollection<MCVPEventStepDto>>(eventStepsToAdd);
var addResult = await this.dbContext.MCVPEvents.AddAsync(eventDto);
// this following line blows up-->
saveResult = await this.dbContext.SaveChangesAsync();
saveResultId = eventDto.Id;
log.LogInformation($"SaveChangesAsync result: {saveResult} and id: {saveResultId}");
return Operation.Success();
}
catch (Exception ex)
{
log.LogInformation($"DbUpdateException: {ex.Message}");
return Operation.Exception(ex);
}
}
but for the life of me I cannot figure out why the container keeps disposing of the context after I call Add... I've tried it with AddAsync and then SaveChangesAsync; I've tried it with just regular non-async Add and then SaveChangesAsync; it only seems to work when I use both of the non-async methods Add and SaveChanges.
Is it because I am calling other methods, like mapper or log during all this? Is it because my return values are not entities?
Don't forget to await the calling method, in my case UpsertEvents(..)!
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
void Update ()
{
if (transform.Translate((Input.acceleration.x), 0, 0))
{
GetComponent<Rigidbody>().AddForce(Vector2.right);
}
}
I am using the accelerator for a Android App and doesn't seem to like it. I can change it to work for KeyDown but it wont work with the accelerator.
Error:
"Cannot implicitly convert type "void" to "bool"
According to the documentation http://docs.unity3d.com/ScriptReference/Transform.Translate.html:
transform.Translate does not return a boolean; its return type is void. Therefore you cannot use an if statement to evaluate whether or not it was successful.
If you want to check to see if the translate happened correctly you would need to check the side effects from calling transform.Translate. In other words see what has changed on the transform and see if it matches your expectations.
Don't use the if statement, as Translate returns nothing. Just leave the AddForce line
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Here is my function:
public void PowerupCollected(int AddScore)
{
score += AddScore;
scoreGUI.text = "lol"+score;
}
Here is how I call that function:
if(other.gameObject.name == "Powerup(Clone)")
{
control.PowerupCollected();
}
here is the error message
error CS1501: No overload for method `PowerupCollected' takes 0 arguments
What is wrong? Is it because I don't include AddScore when in brackets when I call the function?
Either add the AddScore argument to your call (say control.PowerupCollected(42); or make the argument optional: public void PowerupCollected(int AddScore = 0).
Since second solution doesn't make sense in your case, i'd use first one.
Your function call should include the amount of score you want to add:
if(other.gameObject.name == "Powerup(Clone)")
{
control.PowerupCollected();
}
should be (for example):
if(other.gameObject.name == "Powerup(Clone)")
{
control.PowerupCollected(10);
}
This would add 10 to your score.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Simple question I think. I am trying to pass in a list that I created within my Main into a method. I am missing something but can't quite put my finger on it.
Declaration
List<string> emulationList = new List<string>();
Method Call
sataHeader = ParseSataHeader(sataHeader, bcuFileName, List<string> emulationList);
Method Implementation
private static string ParseSataHeader(string sataHeader, string bcuFileName, List<string> emulationList)
{
//some code
}
You don't specify the type when passing arguments.
Change your second snippet to:
sataHeader = ParseSataHeader(sataHeader, bcuFileName, emulationList);
And it will work just fine.