C
C#13mo ago
Mamad

some problems with async await

hey guys before anything sorry for my bad english but here's my problems: 1. how can i add an item to a listbox in an async event method of a bindinglist's listchanged event. 2. whe i have one async method in my code there is no problems but when i have 2 or more async methods no matter wich one is running or one of them is running my app thows an exeption in delegate class and usually it's "parameter is not valid" and i have one guess and that is when in a try catch block some error happens it show me "parameter not valid" exeption in delegate class. i will be appreciated if you guys can help me
39 Replies
Vigil Light
Vigil Light13mo ago
I can guide you
Mamad
MamadOP13mo ago
thank you so much so what should i do?
Vigil Light
Vigil Light13mo ago
First show me your problem with code
JakenVeina
JakenVeina13mo ago
$code
MODiX
MODiX13mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Mamad
MamadOP13mo ago
void list_ListChanged(object sender, ListChangedEventArgs e)
{
listBox1.DataSource = null;
listBox1.DataSource = list;
if (listBox1.Items.Count > 0)
{
//listBox1.SelectedItem = null;
//listBox1.SelectedItem = listBox1.Items[listBox1.Items.Count - 1];
listBox1.SelectedIndex = listBox1.Items.Count - 1;
}

}
void list_ListChanged(object sender, ListChangedEventArgs e)
{
listBox1.DataSource = null;
listBox1.DataSource = list;
if (listBox1.Items.Count > 0)
{
//listBox1.SelectedItem = null;
//listBox1.SelectedItem = listBox1.Items[listBox1.Items.Count - 1];
listBox1.SelectedIndex = listBox1.Items.Count - 1;
}

}
i have bindinglist named list and this method is it's listchanged event and i add content to my binding list in
bool RegisterPerson(Person p)
bool RegisterPerson(Person p)
method and
RegisterPerson
RegisterPerson
method is called like this in a button's click event
private async void button1_Click(object sender, EventArgs e)
{
await Task.Run(() => { StartBot(SavedPeopleForm.people); });
}
private async void button1_Click(object sender, EventArgs e)
{
await Task.Run(() => { StartBot(SavedPeopleForm.people); });
}
JakenVeina
JakenVeina13mo ago
threading exception?
Mamad
MamadOP13mo ago
this is for the listbox and it doesn't throw any exception neither it doesn't update the listbox
JakenVeina
JakenVeina13mo ago
sooo thst's probably because it's throwing an exception
private async void button1_Click(object sender, EventArgs e)
{
await Task.Run(() => { StartBot(SavedPeopleForm.people); });

var x = 0;
}
private async void button1_Click(object sender, EventArgs e)
{
await Task.Run(() => { StartBot(SavedPeopleForm.people); });

var x = 0;
}
set a breakpoint on var x also on the await
Mamad
MamadOP13mo ago
it didn't worked let me send you the exception System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.' inner exception Parameter is not valid and it throws the exception in delegate class and the final line of the
protected virtual object DynamicInvokeImpl(object[] args)
{
RuntimeMethodInfo runtimeMethodInfo = (RuntimeMethodInfo)RuntimeType.GetMethodBase(methodHandle: new RuntimeMethodHandleInternal(GetInvokeMethod()), reflectedType: (RuntimeType)GetType());
return runtimeMethodInfo.UnsafeInvoke(this, BindingFlags.Default, null, args, null);
}
protected virtual object DynamicInvokeImpl(object[] args)
{
RuntimeMethodInfo runtimeMethodInfo = (RuntimeMethodInfo)RuntimeType.GetMethodBase(methodHandle: new RuntimeMethodHandleInternal(GetInvokeMethod()), reflectedType: (RuntimeType)GetType());
return runtimeMethodInfo.UnsafeInvoke(this, BindingFlags.Default, null, args, null);
}
method
JakenVeina
JakenVeina13mo ago
what's the inner exception?
Mamad
MamadOP13mo ago
Parameter is not valid
JakenVeina
JakenVeina13mo ago
where?
Mamad
MamadOP13mo ago
in the last line of this method in the delegate class
JakenVeina
JakenVeina13mo ago
no the inner exception
Mamad
MamadOP13mo ago
This exception was originally thrown at this call stack: System.Drawing.Bitmap.Bitmap(System.IO.Stream) Kambiznet_bot.Bot.BotForm.TakeScreenshot(string, string) in BotForm.cs Kambiznet_bot.Bot.BotForm.CaptchaSolver(Microsoft.ML.ITransformer, Microsoft.ML.MLContext, string) in BotForm.cs Kambiznet_bot.Bot.BotForm.RegisterPerson(Kambiznet_bot.Models.Person) in BotForm.cs Kambiznet_bot.Bot.BotForm.StartBot(System.Collections.Generic.List<Kambiznet_bot.Models.Person>) in BotForm.cs Kambiznet_bot.Bot.BotForm.button1_Click.AnonymousMethod__20_0() in BotForm.cs System.Threading.Tasks.Task.Execute() in Task.cs Kambiznet_bot.Bot.BotForm.button1_Click(object, System.EventArgs) in BotForm.cs
JakenVeina
JakenVeina13mo ago
Kambiznet_bot.Bot.BotForm.TakeScreenshot(string, string) in BotForm.cs
Kambiznet_bot.Bot.BotForm.TakeScreenshot(string, string) in BotForm.cs
where
System.Drawing.Bitmap.Bitmap(System.IO.Stream)
System.Drawing.Bitmap.Bitmap(System.IO.Stream)
is being called
JakenVeina
JakenVeina13mo ago
ArgumentException
stream does not contain image data or is null.

-or-

stream contains a PNG image file with a single dimension greater than 65,535 pixels
ArgumentException
stream does not contain image data or is null.

-or-

stream contains a PNG image file with a single dimension greater than 65,535 pixels
Mamad
MamadOP13mo ago
Bitmap clone;

using (var ms = new MemoryStream(bytes))
{
clone = new Bitmap(ms);

clone = new Bitmap(clone, new Size(390, 90));

AdjustContrast(clone, 0.5f);
AdjustExposure(clone, -0.4f);

clone = clone.Clone(new Rectangle(0, 0, 230, 90), clone.PixelFormat);



CropAndSaveScreenshot(clone, SavePathAndName + 1 + ".jpeg", x1, y1, width1, height1);
CropAndSaveScreenshot(clone, SavePathAndName + 2 + ".jpeg", x2, y2, width2, height2);
CropAndSaveScreenshot(clone, SavePathAndName + 3 + ".jpeg", x3, y3, width3, height3);
CropAndSaveScreenshot(clone, SavePathAndName + 4 + ".jpeg", x4, y4, width4, height4);
clone.Save("temp/test.jpeg");
clone.Dispose();
}
Bitmap clone;

using (var ms = new MemoryStream(bytes))
{
clone = new Bitmap(ms);

clone = new Bitmap(clone, new Size(390, 90));

AdjustContrast(clone, 0.5f);
AdjustExposure(clone, -0.4f);

clone = clone.Clone(new Rectangle(0, 0, 230, 90), clone.PixelFormat);



CropAndSaveScreenshot(clone, SavePathAndName + 1 + ".jpeg", x1, y1, width1, height1);
CropAndSaveScreenshot(clone, SavePathAndName + 2 + ".jpeg", x2, y2, width2, height2);
CropAndSaveScreenshot(clone, SavePathAndName + 3 + ".jpeg", x3, y3, width3, height3);
CropAndSaveScreenshot(clone, SavePathAndName + 4 + ".jpeg", x4, y4, width4, height4);
clone.Save("temp/test.jpeg");
clone.Dispose();
}
JakenVeina
JakenVeina13mo ago
so, bytes appears to be invalid data what's in it?
Mamad
MamadOP13mo ago
Image screenshotPath1 = Image.FromFile("temp/cropped1.jpeg");
Image screenshotPath2 = Image.FromFile("temp/cropped2.jpeg");
Image screenshotPath3 = Image.FromFile("temp/cropped3.jpeg");
Image screenshotPath4 = Image.FromFile("temp/cropped4.jpeg");




byte[] bytes1 = null;
byte[] bytes2 = null;
byte[] bytes3 = null;
byte[] bytes4 = null;

bytes1 = ImageToByteArray(screenshotPath1);
bytes2 = ImageToByteArray(screenshotPath2);
bytes3 = ImageToByteArray(screenshotPath3);
bytes4 = ImageToByteArray(screenshotPath4);

screenshotPath1.Dispose();
screenshotPath2.Dispose();
screenshotPath3.Dispose();
screenshotPath4.Dispose();
Image screenshotPath1 = Image.FromFile("temp/cropped1.jpeg");
Image screenshotPath2 = Image.FromFile("temp/cropped2.jpeg");
Image screenshotPath3 = Image.FromFile("temp/cropped3.jpeg");
Image screenshotPath4 = Image.FromFile("temp/cropped4.jpeg");




byte[] bytes1 = null;
byte[] bytes2 = null;
byte[] bytes3 = null;
byte[] bytes4 = null;

bytes1 = ImageToByteArray(screenshotPath1);
bytes2 = ImageToByteArray(screenshotPath2);
bytes3 = ImageToByteArray(screenshotPath3);
bytes4 = ImageToByteArray(screenshotPath4);

screenshotPath1.Dispose();
screenshotPath2.Dispose();
screenshotPath3.Dispose();
screenshotPath4.Dispose();
i used bytes only in this part
JakenVeina
JakenVeina13mo ago
okay which one is bytes?
Mamad
MamadOP13mo ago
what do you mean exactly?
SinFluxx
SinFluxx13mo ago
using (var ms = MemoryStream(bytes)) As in what are you passing to the MemoryStream
Mamad
MamadOP13mo ago
nothing
using (MemoryStream ms = new MemoryStream())
{
imageIn.Save(ms, imageIn.RawFormat);
return ms.ToArray();
}
using (MemoryStream ms = new MemoryStream())
{
imageIn.Save(ms, imageIn.RawFormat);
return ms.ToArray();
}
should i pass the byte array to the memorystream?
SinFluxx
SinFluxx13mo ago
I was quoting the code you posted...
SinFluxx
SinFluxx13mo ago
No description
Mamad
MamadOP13mo ago
oh ok i see
var bytes = Convert.FromBase64String(Image);
var bytes = Convert.FromBase64String(Image);
SinFluxx
SinFluxx13mo ago
And what's Image?
Mamad
MamadOP13mo ago
public class GetHtmlW
{

public static string G(string Html, string Value, string End)
{
int I = Html.IndexOf(Value);
string WS = "";

if (I > -1)
{
int W = I + Value.Length;
WS = Html.Substring(W, Html.IndexOf(End, W) - W);

}


return WS;
}
}
public class GetHtmlW
{

public static string G(string Html, string Value, string End)
{
int I = Html.IndexOf(Value);
string WS = "";

if (I > -1)
{
int W = I + Value.Length;
WS = Html.Substring(W, Html.IndexOf(End, W) - W);

}


return WS;
}
}
var HtmlW = driver.FindElement(By.XPath("/html/body")).GetAttribute("innerHTML").ToString();


string Image = GetHtmlW.G(HtmlW, $"id=\"{captchaId}\" src=\"data:image/jpeg;base64,", "\"");
var HtmlW = driver.FindElement(By.XPath("/html/body")).GetAttribute("innerHTML").ToString();


string Image = GetHtmlW.G(HtmlW, $"id=\"{captchaId}\" src=\"data:image/jpeg;base64,", "\"");
it extracts an image from a website and turn it into byte array and then to Image
SinFluxx
SinFluxx13mo ago
Have you checked what it's actually returning when it tries to get the image?
Mamad
MamadOP13mo ago
in G method?
SinFluxx
SinFluxx13mo ago
Yeah, have you debugged all the way through to see its doing what you think it's doing?
Mamad
MamadOP13mo ago
to be honest i have no idea because i copied this part of code but i cheaked it before and i didn't understand what it's doing
SinFluxx
SinFluxx13mo ago
I mean I would suggest you need to set some breakpoints and follow it through step by step, see if things are actually getting set correctly/try and understand the code too
Mamad
MamadOP13mo ago
ok it returns a string with some none sense content but it will correctly save and crop images
Will
Will13mo ago
is your base64 string actually a valid image? try pasting it in here https://base64.guru/converter/decode/image
Mamad
MamadOP13mo ago
i cheaked it in that website and it was valid
Want results from more Discord servers?
Add your server