首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

异步委托有关问题!先多谢大家了

2012-04-03 
异步委托问题!请指教!先谢谢大家了上代码:HTMLform idform1 runatserverdiv idDiv_1 runats

异步委托问题!请指教!先谢谢大家了
上代码:HTML
<form id="form1" runat="server">
   
 <div id="Div_1" runat="server">
 <asp:TextBox ID="txtNum" runat="server" Width="388px"></asp:TextBox>
  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  <br />
 <asp:Button ID="Bu1" Text="确定" runat="server" onclick="Bu1_Click" />

</div>
<div id="Div_2" runat="server" >
  <asp:HyperLink ID="HyperLink1" runat="server">取消</asp:HyperLink>
</div>
  </form>
后台代码:
  delegate void DelegateName(Export excel);//定义委托
  /// <summary>
  /// 子线程
  /// </summary>
  /// <param name="excel"></param>
  private void textboxnum( Export excel) 
  {
  for (int i = 0; i < 10; i++)
  {
  Label1.Text += i;
  }  
   
  }
  /// <summary>
  /// 显示
  /// </summary>
  /// <param name="excel"></param>
  private void Vstrue(Export excel) {
  DelegateName dn = new DelegateName(textboxnum);
  AsyncCallback ac = new AsyncCallback(Vsfalse);
  dn.BeginInvoke(excel,ac,dn);
  Div_1.Style.Value = "display:block;";
  Div_2.Style.Value = "display:none;";
   
  }
  /// <summary>
  /// /隐藏图片
  /// </summary>
  private void Vsfalse(IAsyncResult iay) {

  Div_1.Style.Value = "display:none;";
  Div_2.Style.Value = "display:block;";
   
  DelegateName dn = iay.AsyncState as DelegateName;
  dn.EndInvoke(iay);
  }

  protected void Bu1_Click(object sender, EventArgs e)
  {
   
  Export excel = new Export();
  Vstrue(excel);
  }
  }
为什么最后在运行了回调函数之后并没有相应的结果,页面还是显示Vstrue方法里面的结果

[解决办法]
楼主是指这个吗?
Div_1.Style.Value = "display:none;";
Div_2.Style.Value = "display:block;";

我认为是不在UI线程的缘故。用this.dispatch.invoke试试
[解决办法]
dn.BeginInvoke(excel,ac,dn);
DelegateName dn = iay.AsyncState as DelegateName;
要取得委托,其实可以不用把dn传递进去的,还可以这样转换得到
var asyResult = (System.Runtime.Remoting.Messaging.AsyncResult)iay;
var dn = (DelegateName)asyResult.AsyncDelegate;

dn.BeginInvoke(excel,ac,dn);
Div_1.Style.Value = "display:block;";
Div_2.Style.Value = "display:none;";

这三行代码和Vsfalse里的代码运行顺序是不一定的

热点排行