c#线程和等待表单

c#线程和等待表单,c#,multithreading,C#,Multithreading,我连接到webserive。当webservice连接时,我希望有一个带有动画gif的等待表单。等待表单已正确显示,但已设置动画的give未设置动画,而是已固定 有人能帮我吗。我已经尝试过:DoEvents,但gif仍然没有动画 // Create the new thread object Thread NewThread = new Thread(new ThreadStart(RunThread)); // Start

我连接到webserive。当webservice连接时,我希望有一个带有动画gif的等待表单。等待表单已正确显示,但已设置动画的give未设置动画,而是已固定

有人能帮我吗。我已经尝试过:DoEvents,但gif仍然没有动画

        // Create the new thread object
        Thread NewThread = new Thread(new ThreadStart(RunThread));           

        // Start the new thread.
        NewThread.Start();

        // Inform everybody that the main thread is waiting
        FRM_Wait waitingDialog = new FRM_Wait();
        waitingDialog.Show();
        waitingDialog.Activate();
        Application.DoEvents(); 

        // Wait for NewThread to terminate.
        NewThread.Join();

        // And it's done.
        waitingDialog.Close();
        MessageBox.Show("Upload erfolgreich erledigt.", "Upload Erfolgreich",
             MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
    }

    public void RunThread()
    {         
         mfsportservicedev.ServiceSoapClient servicedev = new mfsportservicedev.ServiceSoapClient();

        int status = servicedev.addEvent(videosNames, videos);         
    }

不要从UI线程中对线程调用
Join
。相反,在任务完成之前,禁用您不想操作的任何控件(例如按钮),然后在操作完成后调用UI线程-因此将“and is done”代码移动到一个新方法中,该方法在操作结束时调用。如果您使用的是.NET4,我建议您为此使用TPL,因为它可以更容易地表示“正在进行的任务”并向其添加延续。(这也是在.NET 4.5中实现异步操作的惯用方法的一个良好开端。)

不要从UI线程中调用线程上的
Join
。相反,在任务完成之前,禁用您不想操作的任何控件(例如按钮),然后在操作完成后调用UI线程-因此将“and is done”代码移动到一个新方法中,该方法在操作结束时调用。如果您使用的是.NET4,我建议您为此使用TPL,因为它可以更容易地表示“正在进行的任务”并向其添加延续。(对于将成为.NET 4.5中异步操作惯用方式的操作,这也是一个良好的开端。)

问题来自于您的加入。join是同步的,所以基本上你要让你的UI等待线程完成它的工作

您希望使用回调函数返回UI


编辑:我被炒鱿鱼了

问题来自你的加入。连接是同步的,所以基本上,您正在让UI等待线程完成工作

您希望使用回调函数返回UI

编辑:我被问到了你的问题在这里:

NewThread.Join();
这将阻止UI线程,直到NewThread结束

这里有一种方法:

private myDelegate;
// ...
myDelegate = new Action(RunThread);
myDelegate.BeginInvoke(new AsyncCallback(MyCallback),null);
// You RunThread method is now running on a separate thread
// Open your wait form here
// ...
// This callback function will be called when you delegate ends
private void MyCallback(IAsyncResult ar)
{
     myDelegate.EndInvoke(ar);
     // Note this is still not the UI thread, so if you want to do something with the UI you'll need to do it on the UI thread.
     // using either Control.Invoke (for WinForms) or Dispatcher.Invoke (for WPF)
}
你的问题在这里:

NewThread.Join();
这将阻止UI线程,直到NewThread结束

这里有一种方法:

private myDelegate;
// ...
myDelegate = new Action(RunThread);
myDelegate.BeginInvoke(new AsyncCallback(MyCallback),null);
// You RunThread method is now running on a separate thread
// Open your wait form here
// ...
// This callback function will be called when you delegate ends
private void MyCallback(IAsyncResult ar)
{
     myDelegate.EndInvoke(ar);
     // Note this is still not the UI thread, so if you want to do something with the UI you'll need to do it on the UI thread.
     // using either Control.Invoke (for WinForms) or Dispatcher.Invoke (for WPF)
}

Thread.Join
是一个阻塞调用,它不会输出消息,因此这就是您的问题所在。通常建议避免调用导致UI线程阻塞的任何类型的同步机制

下面是一个使用
Task
类和
Invoke
封送技术的解决方案

private void async InitiateWebService_Click(object sender, EventArgs args)
{
  FRM_Wait waitingDialog = new FRM_Wait();
  waitingDialog.Show();

  Task.Factory.StartNew(
    () =>
    {
      mfsportservicedev.ServiceSoapClient servicedev = new mfsportservicedev.ServiceSoapClient();
      int status = servicedev.addEvent(videosNames, videos);
      waitingDialog.Invoke(
        (Action)(() =>
        {
          waitingDialog.Close();
        }));
    });
}
下面是一个使用原始
线程
的解决方案

private void async InitiateWebService_Click(object sender, EventArgs args)
{
  FRM_Wait waitingDialog = new FRM_Wait();
  waitingDialog.Show();

  var thread = new Thread(
    () =>
    {
      mfsportservicedev.ServiceSoapClient servicedev = new mfsportservicedev.ServiceSoapClient();
      int status = servicedev.addEvent(videosNames, videos);
      waitingDialog.Invoke(
        (Action)(() =>
        {
          waitingDialog.Close();
        }));
    });

  thread.Start();
}
C#5.0通过其新的
async
wait
关键字1,使这种模式更加容易



1尚未发布。

Thread.Join
是一个阻止调用,它不会发送消息,因此这就是您的问题所在。通常建议避免调用导致UI线程阻塞的任何类型的同步机制

下面是一个使用
Task
类和
Invoke
封送技术的解决方案

private void async InitiateWebService_Click(object sender, EventArgs args)
{
  FRM_Wait waitingDialog = new FRM_Wait();
  waitingDialog.Show();

  Task.Factory.StartNew(
    () =>
    {
      mfsportservicedev.ServiceSoapClient servicedev = new mfsportservicedev.ServiceSoapClient();
      int status = servicedev.addEvent(videosNames, videos);
      waitingDialog.Invoke(
        (Action)(() =>
        {
          waitingDialog.Close();
        }));
    });
}
下面是一个使用原始
线程
的解决方案

private void async InitiateWebService_Click(object sender, EventArgs args)
{
  FRM_Wait waitingDialog = new FRM_Wait();
  waitingDialog.Show();

  var thread = new Thread(
    () =>
    {
      mfsportservicedev.ServiceSoapClient servicedev = new mfsportservicedev.ServiceSoapClient();
      int status = servicedev.addEvent(videosNames, videos);
      waitingDialog.Invoke(
        (Action)(() =>
        {
          waitingDialog.Close();
        }));
    });

  thread.Start();
}
C#5.0通过其新的
async
wait
关键字1,使这种模式更加容易



1尚未发布。

那么gif动画在哪里?谁是他的父母?我甚至看不到gif动画的部分。那么gif动画在哪里呢?谁是他的父母?我甚至没有看到动画gif被“skeetified”的部分?哦,讨厌!立即寻求医疗救助,是否“被解雇”?哦,讨厌!立即寻求医疗救助。是的,这是“在GUI事件处理程序中等待”。这比“我的浮点操作给出的是2.999998而不是3.0”更常见是的,它是“在GUI事件处理程序中等待”。这比“我的浮点运算给出的是2.999998而不是3.0”更常见。非常感谢您的回答!你帮了我很多。非常感谢你的回答!你帮了我很多。