tags:

views:

71

answers:

4

Hi! Im trying to call some windows forms code (like setting label.visible = true in some event code, everything compiles ok, but form does not react to change! What could be the problem?

Problem is in lines:

labelNewCall.Visible = true;
timerNewCall.Enabled = true;

code : http://pastebin.com/gV28PN4P

also other code did not work, until i reordered some of it (order is not important but it did not work otherwise... )

+1  A: 

Do you call this method in another than UI thread? If so, you should use Invoke and/or BeginInvoke method.

Look at article What's up with BeginInvoke?.

TcKs
There is separate thread that throws event when data is received, but the event code is in class that is created in Winforms thread. I know that you cant simply call windows forms code from thread that did not create form, but this code does not warn or anything about it so im assuming that it is running on the same thread... Sadly i have Express edition of my IDE so i cant see on what thread its actually running from... Also - some of this winforms code WORKS, some DONT... why there is no consistent behavior? =O And its not only windows forms code - line 90 (file IO) also does not execute...
Oscar
Where is code writen is not important. The important is the current thread and thread where was winforms control/form created.The winforms controls are mainly wrappers around the native windows controls which works with windows messages. The winforms controls hide the complexity (for example Text property sends WM_GETTEXT, WM_SETTEXT messages to the application message queue), but the requirement is using this properties/methods from UI thread.
TcKs
+1  A: 

This could be because the soundCapture_BufferThrown callback function is not run on the GUI thread. Read this post for more details about threading in WinForms.

Darin Dimitrov
A: 

You can also try a handy little method that you can place in your inner-loop:

Application.DoEvents();

Here's the MSDN write up:

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx

Russ C
How could some file IO operations could not be executed if you don't call Application.DoEvents() ?
Oscar
Don't use DoEvents
Joel Coehoorn
Thanks for the mod-down without explaining why *not* to do this.
Russ C
A: 

Use invoke to access object in a windows forms/controls thread

Reference - http://www.dailycoding.com/...formscontrols_thread.aspx

Ramesh Soni