Click here to Skip to main content
15,904,297 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created one application which required long time to complete because client server type application and i want to start new another task at same time.

What I have tried:

private async void button2_Click(object sender, EventArgs e)
{
await Task.Run(() =>
{
Thread.Sleep(1000);
});
MessageBox.Show("Hi from the UI thread!");
}
Posted
Updated 25-Feb-16 20:34pm
Comments
Er. Dinesh Sharma 26-Feb-16 2:32am    
Try this.
private async void button2_Click(object sender, EventArgs e)
{
await Task.Run(() =>
{
Thread.Sleep(1000);
}).ContinueWith(task =>{Console.WriteLine("P");});
MessageBox.Show("Hi from the UI thread!");
}
Use ContinueWith option

1 solution

Thread is the good option to accomplish your task. You can run 2 threads simultaneously,
see below snippet
C#
static void Main(string[] args)
  {
      ThreadStart threadStart1 = new ThreadStart(DoSomething);
      ThreadStart threadStart2 = new ThreadStart(DoSomething);
      Thread th1 = new Thread(threadStart1);
      Thread th2 = new Thread(threadStart2);

      th1.Start();
      th2.Start();

      th1.Join();
      th2.Join();

      Console.ReadLine();
  }

private static void DoSomething()
  {
      while (DateTime.Now < startTime)
      {
          //do nothing
      }

      //both threads will execute code here concurrently
  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900