For now I was using Parallel methods to check urls for 200 status code, but this was slow method. Now I'm trying to move my code to await/async methods but it doesn't working
private async void button1_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
var timerPostingSpeed = new Timer(state =>
{
TimeSpan elapsed = DateTime.Now - start;
string postingSpeed = string.Format("{0:0}",
finishedUrls * 60 / (int)elapsed.TotalSeconds);
UpdatingLabel(label1, postingSpeed);
}, null, 5000, 10000);
IEnumerable<string> urls = File.ReadLines("urls.txt");
var engine = Python.CreateEngine();
var scriptSource =
engine.CreateScriptSourceFromString(#"
if Open(__URL).Result:
print 1
");
await urls.ForEachAsync(20, async line =>
{
try
{
var adderEngine = new SpeedEngine();
// int zz = await adderEngine.Open(line);
// return;
ScriptScope scope = engine.CreateScope();
scope.SetVariable("__URL", line);
scope.SetVariable("Open",
new Func<string, Task<int>>(adderEngine.Open));
try
{
await scriptSource.Execute(scope);
}
catch (UnboundNameException une)
{
MessageBox.Show(une.Message,
msg.
MySeoBoxForm_startPostingToolStripButton_Click_Unbound_Name_Error,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (SyntaxErrorException see)
{
MessageBox.Show(
string.Format("{0}\nLine: {1}\nCode: {2}",
see.Message,
see.Line, see.GetCodeLine()),
msg.
MySeoBoxForm_startPostingToolStripButton_Click_Syntax_Error,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (MissingMemberException mme)
{
MessageBox.Show(mme.Message, "Missing Member",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentTypeException ate)
{
MessageBox.Show(string.Format("{0}", ate.Message),
msg.
MySeoBoxForm_startPostingToolStripButton_Click_Syntax_Error,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (SystemExitException)
{
}
catch (Exception exc)
{
MessageBox.Show(string.Format("{0}", exc.Message),
msg.
MySeoBoxForm_startPostingToolStripButton_Click_Syntax_Error,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
MessageBox.Show(string.Format("{0}","OK"),
msg.
MySeoBoxForm_startPostingToolStripButton_Click_Syntax_Error,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception exc)
{
MessageBox.Show(string.Format("{0}", exc.Message),
msg.
MySeoBoxForm_startPostingToolStripButton_Click_Syntax_Error,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Interlocked.Increment(ref finishedUrls);
numericUpDown1.Invoke(new MethodInvoker(delegate
{
numericUpDown1.Value++;
}));
}
});
timerPostingSpeed.Dispose();
}
}
public async Task<int> Open(string siteUrl)
{
Uri newUri;
if (!Uri.TryCreate(siteUrl, UriKind.Absolute, out newUri)) return 0;
_uri = _openUri = newUri;
_req = new HttpRequestMessage(HttpMethod.Get, _uri);
_response = await _httpClient.SendAsync(_req);
if (_response == null || !_response.IsSuccessStatusCode)
{
return 0;
}
return 1;
}
I need to use Iron Python - without it (when I uncomment await adderEngine.Open(line); return;) everything works ok. But with Iron Python my app stopped at *_response = await _httpClient.SendAsync(_req);* without errors. Also I noticed when I replace
await urls.ForEachAsync(20, async line =>
with:
await urls.ForEachAsync(1, async line =>
it's working
ForEachAsync: http://blogs.msdn.com/b/pfxteam/archive/2012/03/05/10278165.aspx
Any help?
If your Python code is calling Task.Result, and you're on a UI thread (as appears to be the case), you can cause a deadlock (I explain this fully on my blog). In short, the async method (Open) is trying to resume execution on the UI thread, but the UI thread is blocked by calling Result.
AFAIK, Python does not have async/await support, but you should be able to use ContinueWith (meh) or write some kind of bridge between Task and Twisted's Deferred (much cooler).
Related
I'm using QR Code Scanner in Xamarin App, when it scans the qr code, there are some operation it does which takes about a minute, while it's performing operation, I want to show a loading dialog on the screen. But, it isn't showing on the screen, and elsewhere in the app, it's working perfectly.
Code
var expectedFormat = ZXing.BarcodeFormat.QR_CODE;
var opts = new ZXing.Mobile.MobileBarcodeScanningOptions { PossibleFormats = new List<ZXing.BarcodeFormat> { expectedFormat } };
var scanner = new ZXing.Mobile.MobileBarcodeScanner();
var result = await scanner.Scan(opts);
if (result == null)
{
// code
return null;
}
else
{
using (UserDialogs.Instance.Loading("Processing"))
{
// code
}
}
UPDATE CODE SAMPLE
public async Task Verification(object sender, EventArgs e)
{
try
{
var expectedFormat = ZXing.BarcodeFormat.QR_CODE;
var opts = new ZXing.Mobile.MobileBarcodeScanningOptions { PossibleFormats = new List<ZXing.BarcodeFormat> { expectedFormat } };
var scanner = new ZXing.Mobile.MobileBarcodeScanner();
var result = await scanner.Scan(opts);
if (result == null)
{
// Code
return null;
}
else
{
try
{
// QR Scan Result
string qr_scan = result.Response;
var result = JsonConvert.DeserializeObject<QRScan>(qr_scan);
await CreateConnection();
}
catch (Exception error)
{ }
finally
{
// navigate to next page
await NavigationService.NavigateToAsync<NextViewModel>();
}
}
}
catch (Exception error)
{ }
return null;
}
public async Task CreateConnection()
{
UserDialogs.Instance.Loading("Processing");
if ()
{
try
{
// Code
}
catch (Exception error)
{
// Code
}
finally
{
await CreateFolder(default, default);
}
}
}
public async Task CreateFolder(object sender, EventArgs e)
{
UserDialogs.Instance.Loading("Processing");
try
{
// Code
}
catch (Exception error)
{
// Code
}
return null;
}
You can use Xamarin.Essentials' MainThread class and more specifically - the InvokeOnMainThreadAsync method. The idea of this method is to not only execute a code on the UI/main thread, but to also to await it's code. This way you can have both async/await logic and main thread execution.
try
{
// QR Scan Result
string qr_scan = result.Response;
var result = JsonConvert.DeserializeObject<QRScan>(qr_scan);
await MainThread.InvokeOnMainThreadAsync(() => CreateConnection());
}
catch (Exception error)
{ }
finally
{
// navigate to next page
await NavigationService.NavigateToAsync<NextViewModel>();
}
Keep in mind that if the method CreateConnection takes a long time to execute, then it would be better to execute on the main thread only the dialog presentation (UserDialogs.Instance.Loading("")).
Try something like this
Device.BeginInvokeOnMainThread(async () =>
{
try
{
using (UserDialogs.Instance.Loading(("Processing")))
{
await Task.Delay(300);
//Your Service code
}
}
catch (Exception ex)
{
var val = ex.Message;
UserDialogs.Instance.Alert("Test", val.ToString(), "Ok");
}
});
I was working on my windows form program, and i saw that the login function (linked to a simple button) freeze my application. I searched on internet and i found how to create a task, but i'm not sure about how it works ...
That's my login function, how can i correctly translate it into a task?
string sURL = url + "/login";
string result = null;
await Task.Run(() =>
{
try
{
result = Web_api.MakeRequest("POST", sURL); //return null if there is some error
}
catch(Exception ex)
{
Debug.WriteLine("[frmLogin] --> result: " + result);
}
});
if(result != null)
{
try
{
Login_response accepted = JsonConvert.DeserializeObject<Login_response>(result);
Debug.WriteLine("[frm_Login] --> accepted: " + accepted);
if (accepted.login)
{
//throw new Exception();
Debug.WriteLine("[frm_login]: result " + result);
frmMain frm = new frmMain(); //calling the new form
frm.Show(); //new form is show-up
this.Hide(); //log-in form hide
frm.FormClosed += Frm_FormClosed; //close the form
}
}
//if server is down, or the id or password is wrong
catch (Exception ex)
{
lblLoginError.Visible = true; //pop-up the error label
pbLogin.Visible = false; //hide the progress-bar
this.Style = MetroFramework.MetroColorStyle.Red; //changing the color of the form
Debug.WriteLine("Exception: " + ex);
}
}
else
{
lblLoginError.Visible = true; //pop-up the error label
pbLogin.Visible = false; //hide the progress-bar
this.Style = MetroFramework.MetroColorStyle.Red; //changing the color of the form
}
EDIT: i provided a real (and working) soluction and i followed all the suggestion in the comments ... do you think this could be acceptable?
Execute any potentially long-running code on a background thread using a Task:
private async void btnLogin_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtUser.Text.Trim()) || string.IsNullOrEmpty(txtPassword.Text.Trim()))
{
MessageBox.Show("You must insert a valid user/password format", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//Progress bar start
pbLogin.Visible = true; // BUT THIS PROGRESS BAR I STACK DUE TO STATIC DEFINITON OF LOGIN
//Getting ID + Password
User.username = txtUser.Text;
User.password = txtPassword.Text;
string sURL = Web_api.url + "/login";
try
{
Login_response accepted = await Task.Run(() =>
{
//the following code gets executed on a background thread...
string result = Web_api.MakeRequest("POST", sURL);
Login_response accepted = JsonConvert.DeserializeObject<Login_response>(result);
Debug.WriteLine("[frm_Login] --> accepted: " + accepted);
return accepted;
});
//...and here you are back on the UI thread once the task has completed
if (accepted.login)
{
//throw new Exception();
Debug.WriteLine("[frm_login]: result " + result);
frmMain frm = new frmMain(); //calling the new form
frm.Show(); //new form is show-up
this.Hide(); //log-in form hide
frm.FormClosed += Frm_FormClosed; //close the form
}
}
//if server is down, or the id or password is wrong
catch (Exception ex)
{
lblLoginError.Visible = true; //pop-up the error label
pbLogin.Visible = false; //hide the progress-bar
this.Style = MetroFramework.MetroColorStyle.Red; //changing the color of the form
Debug.WriteLine("Exception: " + ex);
}
}
Event handlers always return void. They are an exception to the rule that says that an async method always should return a Task or a Task<T>.
You can create an async void method. It is actually the correct way to implement async callbacks for events such as button click.
First, let's make an asynchronous login method :
public async Task LoginAsync()
{
try
{
var stream = await _http.GetStreamAsync($"{baseUrl}/login");
var response = await JsonSerializer.DeserializeAsync<LoginResponse>(stream);
if (!response.login)
{
throw new BusinessException<LoginError>(LoginError.LoginDenied);
}
}
catch (HttpRequestException ex)
{
throw new BusinessException<LoginError>(LoginError.LoginFailed, ex);
}
}
Now we implement an asynchronous button callback:
private async void btnLogin_Click(object sender, EventArgs e)
{
try
{
await authentication.LoginAsync().ConfigureAwait(false);
// handle login success here
}
catch (BusinessException<LoginError> ex) when (ex.Error == LoginError.LoginDenied)
{
// handle login denied here
}
catch (BusinessException<LoginError> ex) when (ex.Error == LoginError.LoginFailed)
{
// handle connection failed here
}
}
If you want the LoginAsync() method to do some UI operations (for instance showing a form), you will need to use ConfigureAwait(true). Otherwise, there is a risk that part of the method is executed on a different thread and UI operations will fail.
I have method PatchUpdates which calls CheckConnection method to check the connection to remote pc if true then it will get back to first method to user interface and do some other stuff
I searched and found that I need to use threading so I am creating new thread
but my application hung and stops and nothing happen
please what I have done incorrect ?
thanks
public void PatchUpdates()
{
try
{
foreach (DataGridViewRow OfficeListRow in DGV_OfficeList.Rows)
{
string vIPAddress;
string vSoruceFilePath;
int RowNum;
foreach (DataGridViewRow FileListRow in DGV_FileList.Rows)
{
Thread thrd = new Thread(new System.Threading.ThreadStart(PatchUpdates));
thrd.Start();
vIPAddress = OfficeListRow.Cells[1].Value.ToString();
vSoruceFilePath = FileListRow.Cells[4].Value.ToString();
RowNum = OfficeListRow.Index;
///Check the connection to pc
if (CheckConnection(vIPAddress) == true)
{
//MessageBox.Show(vIPAddress + " Pingable ");
DGV_OfficeList[2, RowNum].Value = "Online";
OfficeListRow.DefaultCellStyle.BackColor = Color.LightGreen;
}
else
{
//MessageBox.Show(vIPAddress + " Not Pingable ");
DGV_OfficeList[2, RowNum].Value = "Offline";
OfficeListRow.DefaultCellStyle.BackColor = Color.LightCyan;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public static bool CheckConnection(string IPAddress)
{
bool vPingable = false;
try
{
Ping png = new Ping();
PingReply PngReply = png.Send(IPAddress);
vPingable = PngReply.Status == IPStatus.Success;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return vPingable;
}
You are passing PatchUpdates to the ThreadStart delegate from within the PatchUpdates() method.
Thread thrd = new Thread(new System.Threading.ThreadStart(PatchUpdates));
thrd.Start();
This means that PatchUpdates() method starts itself over again on a new second thread, where it will start itself over again on a new third thread, where it will start itself over again on a new fourth thread, an so on...
Basically you are starting infinite new threads (as long as there are items in DGV_FileList.Rows), which will consume all your resources eventually.
First of all, I apologize for my English.
I have a method to verify my connection to SQL (TestConneccion), and I use a timer to verify it every second.
The problem is when I lose the connection, my application hangs while it tries to connect. I'm using task to avoid this, but I am new to C#.
I would really appreciate the help
public string testConeccion()
{
var archivo = "";
try
{
odb = DatabaseFactory.CreateDatabase("TESTCONECTION");
ocn = odb.CreateConnection();
if (ocn.State == ConnectionState.Closed)
{
ocn.Open();
}
ocn.Close();
archivo = "true";
}
catch (InvalidOperationException ex)
{
archivo = ex.Message;
}
catch (Exception ex)
{
archivo = ex.Message;
}
finally
{
ocn.Close();
}
return archivo;
}
private void timerMesas_Tick(object sender, EventArgs e)
{
Task<string> T1 = Task.Run<string>(() => oClasePublica.testConeccion());
if (T1.Result == "true")
{
btnEstado.Image = new System.Drawing.Bitmap(TOUCHREST.Properties.Resources.Status_32x32);
}
else
{
btnEstado.Image = new System.Drawing.Bitmap(TOUCHREST.Properties.Resources.Warning_32x32);
}
}
Make your timer tick handler async and await your task. This will run the method asynchronously and prevent the rest of your application from hanging while it runs.
private async void timerMesas_Tick(object sender, EventArgs e)
{
string T1 = await Task.Run<string>(() => oClasePublica.testConeccion());
if (T1 == "true")
{
btnEstado.Image = new System.Drawing.Bitmap(TOUCHREST.Properties.Resources.Status_32x32);
}
else
{
btnEstado.Image = new System.Drawing.Bitmap(TOUCHREST.Properties.Resources.Warning_32x32);
}
}
The ocn.Close() in the try code is not needed because the finally will do it. Also catch (InvalidOperationException ex) is not needed because catch (Exception) covers it.
You cannot do Task.Run and then immediately test the Result because the task has not yet finished! Normally you use Task.Wait(); however this will then block your main thread.
I suggest using a BackgroundWorker that continuously checks the connection and then reports it progress. This avoids the issue of changing the image on a non-UI thread.
I have 2 Methods, One method call the other. thread is declared inside the 2nd method and that method will return a boolean output. so when i call the 2nd method i cant control the output, and 1st method returns the success message before the thread ends. I want the boolean output after when the thread ends. How can i control this?
1st Method
private void AccessElements()
{
TaxoProcess Taxo = new TaxoProcess();
if (Taxo.AccessEntity())
{
MessageBox.Show("Succesfully Extracted Data", "Extract Application", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
2nd Method,
public bool AccessEntity()
{
try
{
bool Status = true;
Thread MainThread = Thread.CurrentThread;
Thread backgroundThread = new Thread(new ThreadStart(() =>
{
for (int i = 0; i < Entities.Count; i++)
{
Thread.Sleep(100);
Dispatcher.FromThread(MainThread).BeginInvoke(new Action(() =>
{
int PercentageValue = (int)(0.5f + ((100f * i) / Entities.Count));
StaticDataProperties.ProgBar.Value = PercentageValue;
}));
}
}));
backgroundThread.Start();
return Status;
}
catch (Exception ex)
{
ErrorException = ex.Message;
return false;
}
}
To fix your problem you could use Thread.Join and do this, place this logic before return statement..
backgroundThread.Join(); // blocks calling thread.
There's two general directions you can attempt to take:
Dirty approach:
Add a temprary boole that is true while the "inner" thread is still running and handle the logic that should be handled after this after a "while"-statement on that boole.
(the while-statement would keep the code looping/"paused" untill the boole's value is falsey)
Neat approach:
Using async tasks and a callback function.
Instead of using threads which use too much memory you can use Tasks and async/await like Charles Mager suggested
private async void AccessElements()
{
TaxoProcess Taxo = new TaxoProcess();
if (await Taxo.AccessEntity())
{
MessageBox.Show("Succesfully Extracted Data", "Extract Application", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
public async Task<bool> AccessEntity()
{
return Task.Run(() =>
{
try
{
for (int i = 0; i < Entities.Count; i++)
{
await Task.Delay(100);
int PercentageValue = (int)(0.5f + ((100f * i) / Entities.Count));
StaticDataProperties.ProgBar.Value = PercentageValue;
}
return true;
}
catch (Exception ex)
{
ErrorException = ex.Message;
return false;
}
});
}
This is shorter and does all the thread managements in the background