In my application I'm getting data from another source and uploading those data to the tables.
So I wrote it as,(there are some codes before that, but I start with what I want to ask)
StartProcess();
FirstMonth();
SecondMonth();
ThirdMonth();
private async void StartProcess()
{
try
{
var progress = new Progress<int>(value => { progressBar1.Value = value; });
await System.Threading.Tasks.Task.Run(() => SaveData(progress));
MessageBox.Show("Upload Complete");
}
catch (Exception ex)
{
throw ex;
}
}
private void SaveData(IProgress<int> progress)
{
for (int i = 0; i < dataGridView1.RowCount; i )//data reading one by one from excel
{
string PartNo = dataGridView1.Rows[i].Cells[0].Value.ToString();
string PartDescription = "";
string PartModel = "";
int AvaQty = int.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
DbLoad obj = new DbLoad();
if (obj.UploadStock(PartNo, PartDescription, PartModel, AvaQty))
{
}
else
{
MessageBox.Show("An unexpected error has occurred.Please contact your system administrator");
}
}
MessageBox.Show("Upload Success");
}
This is the FirstMonth Method
private void FirstMonth()
{
try
{
OracleConnection con = new OracleConnection("OraConnection");
con.Open();
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "Query";
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dataGridView1.DataSource = dt.DefaultView;
UploadFirstMonth();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("error" ex);
}
}
private void UploadFirstMonth()
{
for (int i = 0; i < dataGridView1.RowCount; i )
{
string PartNo = dataGridView1.Rows[i].Cells[0].Value.ToString();
int AvaQty = int.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
DbLoad obj = new DbLoad();
if (obj.UpdateFirstMonth(PartNo, AvaQty))
{}
else
{
MessageBox.Show("An unexpected error has occurred on First Month Upload.Please contact your system administrator");
}
}
}
Normally this has more than 15000 records to be upload to the database. Once it uploaded to the database I want to trigger the second method FirstMonth to start the method.
But the current Issue is before finishing the StartProcess() the second method is starting to process. How I stop that ? I want to trigger second method once the first method is completely finished.
CodePudding user response:
If you are tying to wait for the end of the proccess on a sync method on the main thread, then you can simply hit StartProcess().Result
or StartProcess.Wait()
to run those synchronous. You can also call yor second method when all the tasks you need are completed, you can check it here
In the case you are calling the tasks you want to be completed inside an async
method you can simply use the async await
pattern and await StartProcess()
.
I would also suggest you in general to avoid the async void
patter.
Link here
CodePudding user response:
you need to change the return type from void to Task in order to wait for the method to complete since the return type is void it will not wait for completion even if you have awaited the call, and then you need to use await keyword for completing the execution. like below.
async someFunction (){
await StartProcess();
FirstMonth();
}
private async Task StartProcess()
{
try
{
var progress = new Progress<int>(value => { progressBar1.Value = value;
});
await System.Threading.Tasks.Task.Run(() => SaveData(progress));
MessageBox.Show("Upload Complete");
}
catch (Exception ex)
{
throw ex;
}
}
CodePudding user response:
You need to await StartProcess()
before calling FirstMonth()
This will force execution to wait for StartProcess
to finish before it starts FirstMonth
:
await StartProcess();
FirstMonth();
Note that the method that executes these needs to be marked async:
public async Task Foo()
{
await StartProcess();
FirstMonth();
}
You should probably await
both StartProcess
and FirstMonth
though.