这里是我的BeginInvoke调用委托的方法:
public void BindData(string str) { if (string.IsNullOrEmpty(str)) { prev = new PreviewForm(this); if (bottom == null) { bottom = new BottomForm(prev); } if (right == null) { right = new RightForm(this); } prev.Show(this.dockPanel1); right.Show(this.dockPanel1, WeifenLuo.WinFormsUI.DockState.DockRightAutoHide); bottom.Show(this.dockPanel1, WeifenLuo.WinFormsUI.DockState.DockBottomAutoHide); } else { if (str == "wenti") { BeginInvoke(new MethodInvoker(delegate() { bottom.Show(this.dockPanel1, WeifenLuo.WinFormsUI.DockState.DockBottom); right.Show(this.dockPanel1, WeifenLuo.WinFormsUI.DockState.DockRight); })); } else { BeginInvoke(new MethodInvoker(delegate() { right.Show(this.dockPanel1, WeifenLuo.WinFormsUI.DockState.DockRightAutoHide); bottom.Show(this.dockPanel1, WeifenLuo.WinFormsUI.DockState.DockBottomAutoHide); })); } } }
是因为调用的时候需要判断值:
首次加载
private void MainForm_Load(object sender, EventArgs e) { BindData(""); }
接受到主机发送的命令之后传值:
private void Bind(Info_Node node, int vid, int type, string GroupNo) { label1.Visible = true; pic = null; question = null; answer = null; content = null; cour = null; anslist = null; axWindowsMediaPlayer1.currentPlaylist.clear(); if (type == -1) { lblTitle.Visible = false; picTP.Visible = false; lblContent.Visible = false; lblSubject.Visible = true; lblSubject.Text = node.NodeTitle; lblSubject.Location = new Point((this.Width - lblSubject.Width) / 2, (this.Height - lblSubject.Height) / 2); } else { lblTitle.alphaRichTextBox1.Rtf = node.NodeRft; TypeConverter converter = TypeDescriptor.GetConverter(typeof(Point)); lblTitle.Location = (Point)converter.ConvertFromString(node.NodeLocation); TypeConverter converters = TypeDescriptor.GetConverter(typeof(Size)); lblTitle.Size = (Size)converters.ConvertFromString(node.NodeSize); lblTitle.Visible = true; if (type == 0) //视频 { picTP.ImageLocation = ""; picTP.Visible = false; lblContent.Visible = false; lblSubject.Visible = false; cour = Info_CoursewareBLL.GetInfo_Courseware(vid); axWindowsMediaPlayer1.URL = cour.CoursewareURL; axWindowsMediaPlayer1.Visible = true; } else if (type == 1)//文字 { picTP.Visible = false; lblSubject.Visible = false; lblContent.Visible = true; lblContent.Show(); axWindowsMediaPlayer1.Visible = false; content = Info_ContentBLL.GetModel(node.NodeId); if (content != null && content.Contents != "") { lblContent.Location = (Point)converter.ConvertFromString(pointSize(content.CLocation, locOrSize.autoSize)); lblContent.Size = (Size)converters.ConvertFromString(content.CSize); lblContent.alphaRichTextBox1.Rtf = content.Contents; lblContent.Focus(); } } else if (type == 3)//问题 { axWindowsMediaPlayer1.Visible = false; picTP.ImageLocation = ""; picTP.Visible = false; lblSubject.Visible = false; lblContent.Visible = true; content = null; pic = null; question = Info_QuestionBLL.GetModel(node.NodeId); if (question != null && question.QuestionContent != "") { if (question.QuestionSeconds > 0) { UPublic.Timers = question.QuestionSeconds; UPublic.QuesId = question.QuestionId.ToString(); mainform.bottom.TimerStar(); } lblContent.alphaRichTextBox1.Rtf = question.QuestionContent; lblContent.Location = (Point)converter.ConvertFromString(pointSize(question.QLocation.Trim(), locOrSize.location)); lblContent.Size = (Size)converters.ConvertFromString(question.QSize); } //加载(刷新) mainform.BindData("wenti"); } } Thread.Sleep(800); label1.Visible = false; }
这里的主要是在
//加载(刷新)
mainform.BindData("wenti");
这里调用的,这里的界面处理用的dockPanal,目的是为了控制右侧和底部菜单的隐藏和现实,但是现在程序启动之后就会卡死,求大神赐教
跪谢!
Contorl.BeginInvoke 是为了解决跨线程访问控件的多线程同步问题,也就是说当你从不是创建控件的线程访问控件时,你需要使用 BeginInvoke ,可以通过 InvokeRequired 属性来判断是否需要使用方法。
因此,你这里的使用是错误的,我推荐你一种简单的方式,使用 BackgroundWorker 组件。
高人呀,我大清早三小时,终于找到了BackgroundWorker可能会解决,先谢谢大神了,我还没有来得及看用法呢
一语惊醒梦中人,再次感谢