Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

Thread and Task Issue

$
0
0

Hi,

I want to achieve the http request with asynchronous thread.

However I found that if it will block the main thread if I use Task in the new thread.

This is the code I test in winform (I don't think it is the issue of winform, just test in winform)

 public partial class Form1 : Form
    {
        private delegate void TestDe();
        TestDe d;
        public Form1()
        {
            d = new TestDe(TGE2);
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread s = new Thread(new ThreadStart(TGE));
            s.Start();

        }
        private void TGE()
        {
            Thread.Sleep(new TimeSpan(0, 0, 5));
            this.Invoke(d);

        }
         private void TGE2()
         {
             MessageBox.Show("hello");
             Task.WaitAll(PP());
         }
        private async Task PP()
         {
             Thread.Sleep(new TimeSpan(0, 0, 5));
         }
    }

1. First 5 sec, it won't block main thread, it will show hello.

2. After that it will block the main thread!

I don't know why. Please help.

Thanks

Starain


We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>