Start a method when thread ends
I am relatively new to threading, so I am unsure if I am going about this
correctly.
In my current application, I have a multi-tabbed navigational form, some
of which have buttons that lead to other forms, some of which have tables,
and some of which have both. On one such, I have a button to add an entry,
and a table showing all entries for that day pulled via stored procedure.
When I opened that second form as a dialog, it was easy to refresh the
table by calling the method I had created... however, it ran slow like
that. Instead, I started calling it as another thread, which runs
incredibly fast, but I can't seem to find a way to call the method when
the thread ends. I've searched for a while, and I've not found a working
solution.
Here is the code:
public static void ThreadProcAddEntry()
{
Application.Run(new AddEntry());
}
private void btnAdd_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new Thread(new
ThreadStart(ThreadProcAddEntry));
t.Name = "AddEntry";
t.Start();
}
and
public void GetMyEntries(string username)
{
using (SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["DB2"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SP_GetUserEntries",
conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@UserID", SqlDbType.VarChar).Value =
username;
conn.Open();
cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
using (reader)
{
DataTable table = new DataTable();
table.Load(reader);
dgvMyEntry.DataSource = table;
dgvMyEntry.AllowUserToAddRows = false;
dgvMyEntry.ReadOnly = true;
dgvMyEntry.AutoResizeColumns();
dgvMyEntry.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.Fill;
}
}
}
}
How would you suggest I run the GetMyEntries Method when the thread ends,
which occurs as soon as I click the Submit button on the AddEntryForm
(which, after submitting to SQL, runs a dispose method)?
No comments:
Post a Comment