C# 更改静态void GetResponseCallback中的表单标签

C# 更改静态void GetResponseCallback中的表单标签,c#,asynchronous,C#,Asynchronous,嗨,我尝试更改标签元素,但文本没有更改 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e)

嗨,我尝试更改标签元素,但文本没有更改

       public partial class Form1 : Form
        {       
            public Form1()
            {
                InitializeComponent();
            }

           private void button1_Click(object sender, EventArgs e)
            {
                sendPOST("http://example");            
            }

            public static void sendPOST(string URL)
            {
                // Create a request using a URL that can receive a post. 
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(URL);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";

                // Set the Method property of the request to POST.
                request.Method = "POST";

                // start the asynchronous operation
                request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),request);

                allDone.WaitOne();        
            }

private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {

            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            Stream postStream = request.EndGetRequestStream(asynchronousResult);

            // Convert the string into a byte array.
            byte[] byteArray = Encoding.UTF8.GetBytes("");

            // Write to the request stream.
            postStream.Write(byteArray, 0, postData.Length);
            postStream.Close();

            // Start the asynchronous operation to get the response
            request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
        }

    private static void GetResponseCallback(IAsyncResult asynchronousResult)
           {
              //.....
              Form1 test = new Form1();
              test.statusLabel.Text  = responseString; //dont write anything

               Console.WriteLine(responseString); //works well
              //.....
        }
我怎样才能改变它


更新:添加更多代码。我如何获得现有的Form1实例来更新Form1 LabelField,以便在asyn完成后显示一些内容?

从对问题的评论中,我想您可以这样做:将要更改的表单实例作为用户定义的对象
状态
传递给导致调用
GetResponseCallback
的调用(大多数
BeginXYZ
方法允许您传递用户定义的对象,该对象随后处于
IAsyncResult.State
)中。然后您可以执行以下操作:

Form1 test = asynchronousResult.State as Form1;
test.StatusLabel.Text = responseString;
test.Invoke((Action)delegate() { StatusLabel.Text = responseString; });
请注意,由于多线程问题,可能不允许您更改
文本
属性,并且必须使用
控件。调用
来执行此操作。上有许多线程描述了这一点

我现在没有可用的代码示例,但如果我没有弄错的话,它应该是这样的:

Form1 test = asynchronousResult.State as Form1;
test.StatusLabel.Text = responseString;
test.Invoke((Action)delegate() { StatusLabel.Text = responseString; });

如何从静态GetResponseCallback访问现有的Form1实例?@Servy,更新代码,请查看。谢谢。我终于将代码更改为使用更新主UI的线程。BeginInvoke()一次又一次地抛出此命令…请投票人解释他为什么要投票!?