This article reprinted: http://blog.csdn.net/playing9c/article/details/7471918
Http://blog.csdn.net/beelinkerlidejun/article/details/4772491
Http://www.cnblogs.com/fish124423/archive/2012/10/16/2726543.html
C # multithreading of forms has always been a difficult problem, and there must always be strange errors. When developing alexseo software today, it appears that you cannot call invoke or begininvoke on the control before creating a window handle. The main error code is as follows:
protected override void OnLoad(EventArgs e) { base.OnLoad(e); txtRFID_Click(null, null); }
View code
Private void txtrfid_click (Object sender, eventargs e) {thread = new thread () => {If (this. ishandlecreated) This. begininvoke (New methodinvoker () => {this.txt RFID. enabled = false;}); int iret =-1; string strtid = ""; iret = writecardhelper. instance. readtid (ref strtid); // read time-consuming code; Note: time-consuming Code cannot be placed in this. begininvoke (New methodinvoker () => time-consuming code}); // it is executed in; otherwise, no asynchronous effect is generated. Only the code of the operation control can be placed in begininvoke. If (this. ishandlecreated) This. begininvoke (New methodinvoker () => {this.errorprovider.seterror(this.txt RFID, ""); If (0 = iret) {writecardhelper. instance. setalarm (); this.txt RFID. TEXT = strtid; this.txt RFID. backcolor = color. white; this.errorprovider.seterror(this.txt RFID, "");} else {this.txt RFID. TEXT = ""; this.txt RFID. backcolor = color. pink;} this.txt gasbottleno. focus (); this.txt RFID. enabled = true ;})) ;}; thread. isbackground = true; thread. start ();}
Client:
Frmgasbottlesinstall frminstall = new frmgasbottlesinstall (gasbottlesid); frminstall. showdialog (); // asynchronously opens a window.
When the software is suddenly closed during debugging, labb. Invoke (labchange); the statement goes out first, "You cannot call invoke or begininvoke on the control before creating the window handle ." Error. There are two possibilities for this situation. The first is that the interface is too late to respond to invoke, and the second is that the interface thread has ended, so the response cannot be responded. The final solution is to add an IF (labb. ishandlecreated) before labb. Invoke (labchange.
You cannot call invoke or begininvoke on the control before creating a window handle.