473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C# Updating GUI from a static function

17
Hello,

I'm new in C#, and I'm working on some application to do direct audio recording and realtime plotting on screen. Now I'm having a lot of trouble to update the screen yet, it has been taking me almost a week now and been searching everything without result. I hope somebody could help me here..

The detailed problem is as followed:

I have a form and within this form I declared a callback function which is (and has to be) declared as a static to avoid certain "CallbackOnCollectedDelegate" errors. This is handled by initialising the callback as a new type (and this certain type expects a static).
The audio (callback) runs fine, if it's declared as a static though.

Now I receive constant new information within this callback routine and I'd like to send it to the form (gui).
I understand I should use begininvoke to make sure there are no crossthreading problems. Next I try to update the gui from the function which I hand to the begininvoke method.
Yet now the compiler tells me I have to give a static function to the begininvoke method (probably because this begininvoke is within the static callback (which has to be static)).
Now that would be ok, yet if I make the begininvoke-function also a static function, I cannot reach the gui anymore (the "this" keyword is not allowed anymore).
Does this mean it's impossible to reach the GUI from a static function?
Or do I have to make the form also a static? (I read that this is not possible).
Does anyone know how I could tacle this problem? I would be very grateful!

Here's some of the code (without unrelevant stuff):

Expand|Select|Wrap|Line Numbers
  1. namespace APItest
  2. {
  3.     public partial class Form1 : Form   
  4.     {
  5.  
  6.         delegate void UpdateProgressDelegate();
  7.         unsafe public static void* stream;
  8.         unsafe public static PA19.PaStreamCallback callback = new PA19.PaStreamCallback(PACallback);     
  9.  
  10. //-----------------------------------------------------------------------------------------------------------------------------
  11.  
  12.         unsafe public static int PACallback(bla...)
  13.         {
  14.  
  15.             MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
  16.             this.BeginInvoke(mi);
  17.  
  18.  
  19.             return 0;
  20.         }        
  21. //-----------------------------------------------------------------------------------------------------------------------------
  22.  
  23.         void static UpdateProgress()//int time, ref int refParam
  24.         {
  25.  
  26.             this.label1.Text = Convert.ToString(count++);         
  27.         }
  28. //-----------------------------------------------------------------------------------------------------------------------------
  29.  
  30.         unsafe private void cmdStart_Click(object sender, EventArgs e)
  31.         {
  32.  
  33.             error = PA19.PA_OpenDefaultStream(callback);
  34.             error = PA19.PA_StartStream(stream);
  35.         }
  36.  
  37.     }
  38. }
  39.  
Thanks.
-Michiel
Apr 15 '07 #1
4 5768
Your problem is you can't go from object data from a static method. But remember going the opposite way, you CAN access static data from object space. So having said that... One thing comes to my mind is to make a static ArrayList that represents the data records you get from the static callback, Now you can dump data you get on the call back into this ArrayList (Producer)

Next, initiate a worker Thread from your Main form that has a dedicated loop to check the static ArrayList and consume it's data (Consumer). Notice that you are able to access static data from the main form, so that's not a problem.

Next from within your worker thread, you can pass the consumed records to BeginInvoke to update the GUI.

Be sure to **lock** your multi-threaded static ArrayList to prevent deadlocks. And you need to lock it from the SynRoot object to avoid synchronization problems.

Hope this helps...
Apr 17 '07 #2
MichK
17
Hi,
The scheme you mentioned is a posibillity I actually wanted to avoid. I have this allready partly implemented, yet I don't want to be polling my Buffer-array's as the worker-thread shall have to work at very high-speed to provide a smooth update of data to my GUI. This consumes a lot of CPU, so the begininvoke method or some sort of flagging-method would be ideal..
I noticed it's possible to acces form's within a static if you make an instance first though, yet I can't seem to get the instanced form directly linked to the actual form.

Expand|Select|Wrap|Line Numbers
  1. unsafe public static int PACallback
  2. {
  3.   Form1 UserForm = new Form1(); 
  4.   //This should not be "new", yet the compiler won't accept:
  5.   //Form1 UserForm = Form1();
  6.   //Why not? 
  7.  
  8.   MethodInvoker mi = new MethodInvoker(UpdateProgress);
  9.   UserForm.BeginInvoke(mi);
  10. }
  11.  
I'm looking to a semaphore solution now, maby that could solve the problem.

Thanks
-Michiel
Apr 17 '07 #3
MichK
17
I've got the solution.

It amazes me how little is known nor written about this problem, absolutely turned the internet upside-down for over a week without finding any solution nor even people with similar problems.

So it seems I have kind of a pioneering solution to this problem here and therefore I'd like to post it here, I think it would help other people a great deal having similar problems getting very frustrated as I've been these weeks.

The solution is actually pretty simple and solid, although I was actually suprised it worked.

I declared a public static Thread with a non-static callback (apperantly this is allowed).
That's the main gimmick.

Then I start the thread in some global start event (for ex. form_load), yet I suspend/hold the thread immediately and as soon as the Audio (static) callback is called I resume the (non-static) thread-callback.
The thread-callback has a while loop with a thread.suspend at the end, thus acting in "one-shot" mode.
Every time the audio-callback is called it resumes the thread again, thus doing one cycle.
I can, of coarse, do a begininvoke in this non-static thread to make sure the GUI is then called properly and there it is: a working method to call a non-static function from a static thread, updating a GUI in sync.
Who would've known it could be done. not a lot of people obviously...

It's working great now and as far as I can tell it's rock solid.
Apr 19 '07 #4
Hey.

I can see it's quite an old thread this, but ill try anyway.
Would it be possible to se a code example of this solution?

/Rasmus

@MichK
Jun 10 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.  These are part of the problem. I want to have something running in the background, while the...
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
6
by: Hasanain F. Esmail | last post by:
Hi all, I sincerly thank you all in advance for your help to solve this problem. I have been trying to find a solution to this problem for sometime now but have failed. I am working on a...
3
by: Christian Filzwieser | last post by:
Hy I created a WebControl called StatusBar with a static function public static void SetText(string text) { MyVariable = Text } and on Page Load I set MyVariable to the Label in my WebControl....
22
by: Mal Ball | last post by:
I hope I have the right forum for this question. I have an existing Windows application which uses a SQL Server database and stored procedures. I am now developing a web application to use the same...
2
by: UKuser | last post by:
Hi Folks, I havent used javascript in ages, and am not the worlds guru, but I'm playing with ajax linking to my database and an updating area. I have an area named display for example which...
2
by: mark4asp | last post by:
Q: Initialising and updating a class with only static members & database dependency I have a class with the following members: public static List<ACISACIS_List; static AssetClass() { //...
5
by: rosaryshop | last post by:
I'm working a jewelry/rosary design web site at http://www.rosaryshop.com/rosariesAndKits2.php. As the user makes selections, it updates images of various parts, giving them a preview of the...
1
by: alireza6485 | last post by:
Hi,Please help me to answer this question.I tried my best and I am not sure if my code is good or useless. I am writing a program that calculates projectile motion. so the ball is thrown with...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.