tag:blogger.com,1999:blog-8259613.post7306217815258567125..comments2023-08-05T11:16:50.347+02:00Comments on Kristof Verbiest - Bite-size C#: Simple pattern to invoke GUI from another threadKristofhttp://www.blogger.com/profile/01727380410232817527noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-8259613.post-54628215344427657382012-02-15T17:11:05.548+01:002012-02-15T17:11:05.548+01:00Seeeee perfecto, exacto lo que necesitaba, gracias...Seeeee perfecto, exacto lo que necesitaba, gracias!ozkr2805https://www.blogger.com/profile/06324801139187885347noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-17387660293081846162009-08-13T20:54:06.095+02:002009-08-13T20:54:06.095+02:00Really nice post !!!Really nice post !!!Akanshahttps://www.blogger.com/profile/02229682596355341516noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-10064121573326812282009-07-09T09:10:58.618+02:002009-07-09T09:10:58.618+02:00@Anonymous: I would change your code to use BeginI...@Anonymous: I would change your code to use BeginInvoke instead of Invoke. See one of my other posts to see why.Kristofhttps://www.blogger.com/profile/01727380410232817527noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-83701865582698220462009-07-08T21:12:05.611+02:002009-07-08T21:12:05.611+02:00public static class InvokeUtil
{
public static...public static class InvokeUtil<br />{<br /> public static void SafeInvoke(this Control control, MethodInvoker del)<br />{<br /> if (control.InvokeRequired)<br /> {<br /> control.Invoke(delegate);<br /> }<br /> else<br /> {<br /> del();<br /> }<br />}<br /><br />USE:<br />private void SetLabelText(int number)<br />{<br /> this.SafeInvoke(delegate<br /> {<br /> label.Text = number.ToString();<br /> }<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8259613.post-52484122529138004602008-03-16T18:29:00.000+01:002008-03-16T18:29:00.000+01:00This pattern solved my problem with multi threads ...This pattern solved my problem with multi threads and a richtextbox. Clean and simple.<BR/>Thanks<BR/><BR/>JanAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8259613.post-2744249059516607952008-02-27T14:54:00.000+01:002008-02-27T14:54:00.000+01:00Hello,Can some1 explain me, why the "Invalid Threa...Hello,<BR/><BR/>Can some1 explain me, why the "Invalid Thread Operation" is only thrown in debug mode? When I set for example the title unsave and I run the code without debugger, I got no response that there maybe oncurred an error ...<BR/><BR/>Thx for your help. Nice pattern anyway.<BR/><BR/>Best Regards,<BR/>JanAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8259613.post-84412345878862425362007-08-27T23:39:00.000+02:002007-08-27T23:39:00.000+02:00Thanks for the pattern. I was trying to self-teach...Thanks for the pattern. I was trying to self-teach multi-threading with gui and your pattern clarified the many things i was reading.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8259613.post-83261841616704255102007-07-06T10:28:00.000+02:002007-07-06T10:28:00.000+02:00adam: you're getting a stack overflow on the GUI-t...adam: you're getting a stack overflow on the GUI-thread because you keep on invoking the same method - but you don't have any code that actually does something with the RichTextBox control.<BR/><BR/>This is how I would correct your first example:<BR/>delegate void PrintToOutputWindowDelegate(string msg);<BR/>protected void PrintToOutputWindow(string msg)<BR/>{<BR/>if (outputWindow.InvokeRequired)<BR/>{<BR/>outputWindow.BeginInvoke(new PrintToOutputWindowDelegate(PrintToOutputWindow), msg);<BR/>}<BR/><BR/>// print the message in the output window<BR/><B>outputWindow.Text += msg;</B><BR/>}Kristofhttps://www.blogger.com/profile/01727380410232817527noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-87446869290537148982007-07-05T17:45:00.000+02:002007-07-05T17:45:00.000+02:00Ah nevermind. I was calling the function again ins...Ah nevermind. I was calling the function again instead of clearing the output window. =)Unknownhttps://www.blogger.com/profile/04126728605955100665noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-38787477580225969572007-07-05T17:34:00.000+02:002007-07-05T17:34:00.000+02:00I have a quick question for you guys. I was runnin...I have a quick question for you guys. I was running into this illegal cross thread problem recently, so I'm having to refactor my code to use delegates.<BR/><BR/>I'm very new to delegates and very new to C# and winforms, so bear with me =)<BR/><BR/>I have a Deployer class to which I pass a RichTextBox control from my main form like so:<BR/><BR/>Deployer myDeployer = new Deployer(outputWindow);<BR/><BR/>Then, in my Deployer class, I have the following code for clearing and writing to the output window:<BR/><BR/> delegate void PrintToOutputWindowDelegate(string msg);<BR/> protected void PrintToOutputWindow(string msg)<BR/> {<BR/> if (outputWindow.InvokeRequired)<BR/> {<BR/> outputWindow.BeginInvoke(new PrintToOutputWindowDelegate(PrintToOutputWindow), msg);<BR/> }<BR/><BR/> // print the message in the output window<BR/> PrintToOutputWindow(msg);<BR/> }<BR/><BR/> delegate void ClearOutputWindowDelegate();<BR/> protected void ClearOutputWindow()<BR/> {<BR/> if (outputWindow.InvokeRequired)<BR/> {<BR/> outputWindow.BeginInvoke(new ClearOutputWindowDelegate(ClearOutputWindow));<BR/> }<BR/><BR/> // clear the output window<BR/> ClearOutputWindow();<BR/> }<BR/><BR/>But I'm getting a StackOverflowException on the recursive call. Basically, it seems that outputWindow.InvokeRequired is always returning true, even after the BeginInvoke call.<BR/><BR/>Am I doing something wrong?Unknownhttps://www.blogger.com/profile/04126728605955100665noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-14598976945635710012007-04-13T09:24:00.000+02:002007-04-13T09:24:00.000+02:00I prefer a small helper class like this one, it ma...I prefer a small helper class like this one, it makes the code even cleaner and shorter:<BR/><BR/>using System.Windows.Forms;<BR/><BR/>public static class ControlsHelper<BR/>{<BR/> public static void SyncBeginInvoke(control, MethodInvoker del)<BR/> {<BR/> if ((control != null) && control.InvokeRequired)<BR/> control.BeginInvoke(del, null);<BR/> else<BR/> del();<BR/> }<BR/>}<BR/><BR/>private void SetLabelText(int number)<BR/>{<BR/> ControlsHelper.SyncBeginInvoke(this, delegate()<BR/> {<BR/> label.Text = number.ToString();<BR/> });<BR/>}<BR/><BR/>Sorry, don't know how to format the code correctly, pre doesn't work.Gunterhttps://www.blogger.com/profile/04625814567884361265noreply@blogger.com