My problem is, that I have a program with sveral classes, and I want a bitmap sent from my graphic class to the main class, when an event occurs in the graphic class. The program runs fine for a while (2-3) seconds, but then I get this message: "Object is currently in use elsewhere" and it is the bitmap it is referring to.
It is a school projekt to make a digital oscilloscope, so there is a lot of code to the projekt. Therefore it would be confusing if i copy'ed it all in here. So I will try to just copy the important parts.
Hope that you can help me :)
thanks.
Main class:
Expand|Select|Wrap|Line Numbers
- public Main()
- {
- Graphic.InitGraphic();
- Graphic.ChangeOfGrapic += new Graphic.ChangingHandler(GraphicChange);
- Background.Controls.Add(Graph);
- Graph.BackColor = Color.Transparent;
- Graph.Location = new System.Drawing.Point(0, 0);
- Graphic.DrawBackground();
- }
- private void GraphicChange(Bitmap ChangedGrapic)
- {
- Graph.Image = ChangedGrapic;
- }
- Here I have to add that Graph and Background are picturebox'es in my form.
- Grapic class:
- static class Graphic
- {
- public static int x = 0;
- private static Bitmap myBitmapBackground = new Bitmap(600, 600);
- private static Graphics myGraphicsBackground;
- private static Bitmap myBitmapGraph = new Bitmap(600, 600);
- private static Graphics myGraphicsGraph;
- public static event ChangingHandler ChangeOfGrapic;
- public delegate void ChangingHandler(Bitmap test);
- public static void InitGraphic()
- {
- myGraphicsBackground = Graphics.FromImage(myBitmapBackground);
- myGraphicsGraph = Graphics.FromImage(myBitmapGraph);
- }
- public static void ShowMeasurement(float measvalue)
- {
- if (x >= 600 && Functions.mode == 0)
- {
- ClearGraph();
- }
- else if (x >= 600 && Functions.singlerun == false)
- {
- if (Functions.timebase == 10 || Functions.timebase == 1 || Functions.timebase == 0.1)
- {
- ClearGraph();
- }
- }
- else
- {
- SolidBrush graphdot = new SolidBrush(Color.Red);
- myGraphicsGraph.FillEllipse(graphdot, x, (300 - ((300 / 5) * ((measvalue + Functions.offset) / Functions.voltagescale))), 2, 2);
- }
- if (Functions.timebase == 0.1)
- {
- x += 6;
- }
- else
- {
- x += 1;
- }
- ChangeOfGrapic(myBitmapGraph);
- }
It seems to me that both the Main and the Grapic class is using the bitmaps in some sort of way. But correct me if I'm wrong, but they run in the same thread, right?