I'm doing work on some database code. At some point I do need to move these calls off of the UI thread and on to a Background thread. I've got my code posted below but I'm curious as to some examples for doing this or if someone could show how to do this. I've done Async Calls in Java just trying to wrap my head around doing this C# with Visual Studio 2013. Any help would be appreciated.
Database Code :
static public Project.Project QueryProject(string projDatabaseName)
{
Project.Project proj = new Project.Project();
string connStr = "server=localhost;database=" + projDatabaseName + ";user=******;port=3306;password=*****;";
string queryStr = "SELECT * FROM " + projDatabaseName + ".project";
MySqlConnection myConnection = new MySqlConnection(connStr);
MySqlCommand myCommand = new MySqlCommand(queryStr, myConnection);
myConnection.Open();
try
{
MySqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
proj.ProjectID = int.Parse(myReader["ProjectID"].ToString());
proj.ProjectName = myReader["ProjectName"].ToString();
proj.ProjectStartDate = Convert.ToDateTime(myReader["ProjectStartDate"]);
proj.ProjectEndDate = Convert.ToDateTime(myReader["ProjectEndDate"]);
proj.ProjectNotes = myReader["ProjectNotes"].ToString();
}
myReader.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
finally
{
myConnection.Close();
}
return proj;
}
Call to database code:
savedProj = ProjectDbInteraction.QueryProject(currentProjDb);
You should do some research into BackgroundWorker https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28v=vs.110%29.aspx and Threading in general with C#.
I would try using a background worker that would look something like this:
//setting up your background worker.
var worker = new BackgroundWorker();
worker.DoWork += bgw_DoWork;
worker.RunWorkerCompleted += bgw_WorkCompleted;
worker.ProgressChanged += bgw_ProgressChanged;
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = ProjectDbInteraction.QueryProject(currentProjDb);
}
private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//Here you can inspect the worker and update UI if needed.
}
private void bgw_WorkCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//check for errors, and other unexpected results first...
//assuming that savedProj is some private member variable of your class,
//just assign the variable to the Result here.
savedProj = (Project.Project)e.Result;
}
Related
I read both BackgroundWorker still freezes UI and UI still freezes using backgroundWorker but neither have helped me solve my issue. I asked a question a few days ago about Datatable not displaying information after SQL query fill in backgroundworker, which I eventually solved by using Invoke and a delegate.
My current code (shown below) gives me the results I want, except for two things: it doesn't actually display the progress bar (at least that I can see) and it freezes the UI while running:
DataTable tab1table = new DataTable();
public Form1()
{
InitializeComponent();
Instantiatesearch1Thread();
}
private void Instantiatesearch1Thread()
{
search1Thread.WorkerReportsProgress = true;
search1Thread.WorkerSupportsCancellation = true;
search1Thread.ProgressChanged += search1Thread_ProgressChanged;
search1Thread.DoWork += search1Thread_Dowrk;
search1Thread.RunWorkerCompleted += search1Thread_RunWorkerCompleted;
}
private void sbutton1_Click(object sender, EventArgs e)
{
search1Thread.RunWorkerAsync();
}
void search1Thread_Dowrk(object sender, DoWorkEventArgs e)
{
int percentprogress = 0;
var worker = sender as BackgroundWorker;
// Ensure process is thread-safe by using Invoke method
Invoke(new MethodInvoker(delegate
{
// Search1 button event handler
using (SqlConnection conn = new SqlConnection(Connection_Info))
{
conn.Open();
using (SqlDataAdapter cmd = new SqlDataAdapter(comboBox1SQL, conn))
{
//MessageBox.Show("Working");
if (comboBox1.Text.Contains("ID"))
{
long para = long.Parse(search1.Text);
cmd.SelectCommand.Parameters.Add(new SqlParameter
{
ParameterName = "#combo1Par",
Value = para,
SqlDbType = SqlDbType.BigInt
});
}
// Clear datatable if it contains any information and then fill it
if (tab1table != null)
tab1table.Clear();
cmd.Fill(tab1table);
// Set Progress Bar info
search1Progress.Maximum = tab1table.Rows.Count;
groupBox2.Controls.Add(search1Progress);
search1Progress.BringToFront();
}
}
// This is a long calculation where I am reporting the
// progress every iteration (which is probably not helping,
// but didn't change anything when commented out).
if(!String.IsNullOrEmpty(search1.Text) && tab1Combo1.SelectedIndex > -1 || tab1Combo2.SelectedIndex > -1)
{
for (int i = tab1table.Rows.Count - 1; i >= 0; i--)
{
DataRow dr = tab1table.Rows[i];
if (!dr.ItemArray.Contains(search1.Text.ToString()))
{
dr.Delete();
tab1table.AcceptChanges();
}
percentprogress++;
worker.ReportProgress(percentprogress);
}
}
}));
}
void search1Thread_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
search1Progress.Value = e.ProgressPercentage;
}
void search1Thread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
tab1datatable.DataSource = null;
tab1datatable.DataSource = tab1table;
tab1datatable.Update();
groupBox2.Controls.Remove(search1Progress);
MessageBox.Show(" Your search returned " + tab1table.Rows.Count.ToString() + " results");
}
Can anyone point out what I am doing wrong here?
My issue may have something to do with Invoke as I know it's not the best method with BackgroundWorker, but I couldn't get it to update the UI using any other method.
I want to run more query at the really same time in different thread. I am using backgroundworkers to solve this problem! My question is: Is there better way to delegate UI element or is it correct as i did it?
private void mainform_Load(object sender, EventArgs e)
{
if (bscan_backgroundworker.IsBusy == false)
{
bscan_backgroundworker.RunWorkerAsync();
}
if (bscan2_backgroundworker.IsBusy == false)
{
bscan2_backgroundworker.RunWorkerAsync();
}
}
private void bscan_backgroundworker_DoWork(object sender, DoWorkEventArgs e)
{
bscan();
}
private void bscan2_backgroundworker_DoWork(object sender, DoWorkEventArgs e)
{
bscan2();
}
private void bscan()
{
string query = "Select * from table_name";
MySqlConnection mysqlconn_to_db = new MySqlConnection(connectionstring);
try
{
mysqlconn_to_db.Open();
using (MySqlCommand command = new MySqlCommand(query, mysqlconn_to_db))
{
command.ExecuteNonQuery();
using (MySqlDataAdapter adapter = new MySqlDataAdapter(command))
{
DataTable datatable = new DataTable();
adapter.Fill(datatable);
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate ()
{
dataGridView1.DataSource = datatable;
dataGridView1.Refresh();
label2.Text = dataGridView1.Rows[Convert.ToInt16(label1.Text)].Cells[0].Value.ToString();
}));
}
else
{
dataGridView1.DataSource = datatable;
dataGridView1.Refresh();
label2.Text = dataGridView1.Rows[Convert.ToInt16(label1.Text)].Cells[0].Value.ToString();
}
adapter.Dispose();
}
}
}
catch (Exception ex)
{
messagebox(ex.Message);
}
}
bscan2() method is nearly the same as bscan() with different query and different datagridview. Is there more efficient way to do it, or its ok?
Your BackgroundWorker solution isn't necessarily a bad one, but there are new and improved ways to handle asynchronous programming in C#. It's highly recommended that you look into async and await. That may not apply quite so directly to what you're trying to accomplish in this case since you don't seem to want to wait for one method to complete, so it's also recommended that you look into the Task Parallel Library (TPL), and particularly Task.Run(). There's even something called Parallel LINQ that is specifically meant for handling asynchronous queries.
I created a Sql server database, in which I added a table named user. Then I executed this script
ALTER DATABASE [TestNotification] SET ENABLE_BROKER
I'd like to use SqlDependency class to notify a winforms application when the user table were changed.
namespace Watcher
{
public partial class Form1 : Form
{
private int changeCount = 0;
private const string statusMessage = "{0} changes have occurred.";
private DataSet dataToWatch = null;
private SqlConnection connection = null;
private SqlCommand command = null;
public Form1()
{
InitializeComponent();
button1.Enabled = CanRequestNotifications();
this.FormClosed += Form1_FormClosed;
}
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
SqlDependency.Stop(GetConnectionString());
if (connection != null)
{
connection.Close();
}
}
private bool CanRequestNotifications()
{
// In order to use the callback feature of the
// SqlDependency, the application must have
// the SqlClientPermission permission.
try
{
SqlClientPermission perm =
new SqlClientPermission(
PermissionState.Unrestricted);
perm.Demand();
return true;
}
catch
{
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
changeCount = 0;
label1.Text = String.Format(statusMessage, changeCount);
//SqlDependency.Stop(GetConnectionString());
SqlDependency.Start(GetConnectionString());
if (connection == null)
{
connection = new SqlConnection(GetConnectionString());
}
if (command == null)
{
command = new SqlCommand(GetSQL(), connection);
}
if (dataToWatch == null)
{
dataToWatch = new DataSet();
}
GetData();
}
private string GetConnectionString()
{
return #"Data Source=BILOG-PRT-12\SQLEXPRESS; Initial Catalog=TestNotification;Integrated Security=True";
}
private string GetSQL()
{
return "Select [id],[nom],[prenom],[age] from [dbo].[user]";
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
MessageBox.Show("modification Occurred");
ISynchronizeInvoke i = (ISynchronizeInvoke)this;
if (i.InvokeRequired)
{
OnChangeEventHandler tempDelegate =new OnChangeEventHandler(dependency_OnChange);
object[] args = { sender, e };
i.BeginInvoke(tempDelegate, args);
return;
}
SqlDependency dependency = (SqlDependency)sender;
dependency.OnChange -= dependency_OnChange;
++changeCount;
label1.Text = String.Format(statusMessage, changeCount);
GetData();
}
private void GetData()
{
//dataToWatch.Clear();
//command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
if (connection.State != ConnectionState.Open) connection.Open();
using (var dr = command.ExecuteReader())
{
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
}
}
}
}
All the broker service are running :
I launched the application, Then I clicked into the button and finally I go the Sql Server management studio And I inserted a new row. The problem is that the message box in the application is not shown so the c# application is not notified by SQL Server!!!
So I need to know :
Why this happens?
Which step I forget ?
How can I resolve this issue?
There's quite a few limitations with SqlDependency. To quote one relevant issue:
The projected columns in the SELECT statement must be explicitly stated, and table names must be qualified with two-part names.Notice that this means that all tables referenced in the statement must be in the same database.
(see https://msdn.microsoft.com/library/ms181122.aspx for the full list)
You have to explicitly use two-part name (e.g. dbo.user instead of just user).
Also, you need to execute the command. It doesn't just start working automagically :) Adding a simple using (var dr = command.ExecuteReader()) {} should be enough.
Im trying to populate a datagridview from a query of sql but it takes a long time , what im trying to do is show a .gif "loading" meanwhile is populating the grid, im using threads but the .gif freezes , and if I use the CheckForIllegalCrossThreadCalls = false; the datagridview doesnt load the scroll bar acts weird. here is my code
delegate void CambiarProgresoDelegado();
BUTTON CLICK
private void btn_busca_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
thread= new Thread(new ThreadStart(ejecuta_sql));
thread.Start();
}
method
private void ejecuta_sql()
{
if (this.InvokeRequired)
{
CambiarProgresoDelegado delegado = new CambiarProgresoDelegado(ejecuta_sql);
object[] parametros = new object[] { };
this.Invoke(delegado, parametros);
}
else
{
myConnection.Open();
SqlCommand sql_command2;
DataSet dt2 = new DataSet();
sql_command2 = new SqlCommand("zzhoy", myConnection);
sql_command2.CommandType = CommandType.StoredProcedure;
sql_command2.Parameters.AddWithValue("#FechaIni", dateTimePicker1.Value.ToShortDateString());
sql_command2.Parameters.AddWithValue("#FechaFin", dateTimePicker2.Value.ToShortDateString());
SqlDataAdapter da2 = new SqlDataAdapter(sql_command2);
da2.Fill(dt2, "tbl1");
grid_detalle.DataSource = dt2.Tables[0];
myConnection.Close();
pictureBox1.Visible = false;
}
and the .gif freezes until the thread finish his job.
You created a thread but then immediately made the code switch back to the main UI thread with Invoke(), negating any benefits of making the thread in the first place.
Run the query on the other thread, then Invoke() just the part that updates the UI:
private string FechaIni;
private string FechaFin;
private void btn_busca_Click(object sender, EventArgs e)
{
btn_busca.Enabled = false;
pictureBox1.Visible = true;
FechaIni = dateTimePicker1.Value.ToShortDateString();
FechaFin = dateTimePicker2.Value.ToShortDateString();
thread = new Thread(new ThreadStart(ejecuta_sql));
thread.Start();
}
private void ejecuta_sql()
{
myConnection.Open();
SqlCommand sql_command2;
DataSet dt2 = new DataSet();
sql_command2 = new SqlCommand("zzhoy", myConnection);
sql_command2.CommandType = CommandType.StoredProcedure;
sql_command2.Parameters.AddWithValue("#FechaIni", FechaIni);
sql_command2.Parameters.AddWithValue("#FechaFin", FechaFin);
SqlDataAdapter da2 = new SqlDataAdapter(sql_command2);
da2.Fill(dt2, "tbl1");
myConnection.Close();
this.Invoke((MethodInvoker)delegate {
grid_detalle.DataSource = dt2.Tables[0];
pictureBox1.Visible = false;
btn_busca.Enabled = true;
});
}
You may consider changing your approach, especially if you are doing a lot of GUI updates from your background threads. Reasons:
UI update takes time and it slows down background processing as you have to lock
too many updates coming from the background threads will overwhelm the UI and might cause freezes.
you probably don't want to update GUI every millisecond or so
What I prefer is to do polling the background thread data instead. Set up GUI timer for say 300ms, then check if there is any data ready to be updated, then do quick update with proper locking.
Here is code example:
private string text = "";
private object lockObject = new object();
private void MyThread()
{
while (true)
{
lock (lockObject)
{
// That can be any code that calculates text variable,
// I'm using DateTime for demonstration:
text = DateTime.Now.ToString();
}
}
}
private void timer_Tick(object sender, EventArgs e)
{
lock(lockObject)
{
label.Text = text;
}
}
Note, that while the text variable is updated very frequently the GUI still stays responsive. If, instead, you update GUI on each "text" change, your system will freeze.
I am getting a ContextSwitchDeadlock when I try to add a large amount of graphics to a map. I would love to use the BackgroundWorker Class especially since I have never had the opportunity, but I am not sure how to in this situation.
C#:
private void AttributeSearchButton_Click(object sender, RoutedEventArgs e)
{
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(RunQuery);
bw.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
}
private void RunQuery(object sender, DoWorkEventArgs e)
{
_attributeQueryGraphicsLayer.Graphics.Clear();
QueryTask queryTask = new QueryTask("http://xx.x.x.xxx:6080/arcgis/rest/services/Calvert_City_Test/MapServer/2");
queryTask.ExecuteCompleted += GeneralQueryTask_ExecuteCompleted;
queryTask.Failed += GeneralQueryTask_Failed;
var dirty = DateTime.UtcNow;
Query query = new Query();
query.OutFields.Add("*");
query.ReturnGeometry = true;
query.Where = "CIS_METR LIKE '%" + QueryParametersTextbox.Text + "%' OR COMMENTS LIKE '%" + QueryParametersTextbox.Text + "%'";
query.OutSpatialReference = MyMap.SpatialReference;
queryTask.ExecuteAsync(query);
}
// Draw results when query is complete
private void GeneralQueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
{
// Clear previous results
QueryDataGrid.Visibility = Visibility.Visible;
GraphicsLayer graphicsLayer = MyMap.Layers["QueryResults"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
// Check for new results
FeatureSet featureSet = args.FeatureSet;
bool isExceedTransferLimit = args.ExceededTransferLimit;
try
{
if (featureSet.Features.Count > 0 && isExceedTransferLimit == false)
{
// Add results to map
foreach (Graphic resultFeature in featureSet.Features)
{
//resultFeature.Symbol = ResultsFillSymbol;
graphicsLayer.Graphics.Add(resultFeature);
}
}
else
{
MessageBox.Show("No features found");
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
// Notify when query fails
private void GeneralQueryTask_Failed(object sender, TaskFailedEventArgs args)
{
MessageBox.Show("Query failed: " + args.Error);
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.ProgressTextbox.Content = (e.ProgressPercentage.ToString() + "%");
}
I know that the logic is off in this, but I am clueless on how to fix it. I have referred to http://msdn.microsoft.com/en-us/library/System.ComponentModel.BackgroundWorker(v=vs.95).aspx for help with how to use the class, however the information provided is not for this specific situation.
What I am wanting to do:
When the user clicks the Search button, it will Run the query. This works perfectly. The problem is when it runs the GeneralQueryTask_ExecuteCompleted I get a ContextSwitchDeadlock message if there are a large amount of records. This also leaves the UI unresponsive.
From what I have read I would need to put:
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
where the time consuming task takes place. The problem is that there is no overloaded method for me to be able to do the QueryTask and the BackgroundWorker. Any help would be appreciated. If you need more information, please let me know.