Watek i delegaty

0

Witam wszystkich, czy mogłby ktoś pomóc zrobić delegata aby ten watek o nazwie watek() mogł działac, poprawnie bo niemoge sobie sam z tym poradzic. Chodzi tu o rysowanie na picturebox pixeli

 public partial class Form1 : Form
{
    Bitmap bmp;
   public Graphics g;
    
    void watek()
    {
        while (true)
        {
            g = Graphics.FromImage(bmp);
            for (int i = 0; i < 100; i++)
            {
                //
               //obliczm zmiena odpowiedz, x y
               //
                Pen z = new Pen(Color.Green, 1.0f);
                Pen r = new Pen(Color.Red, 1.0f);
                if (odpowiedz > 0)
                {
                    g.DrawEllipse(z, x, y, 1, 1);
                }
                else
                {
                    g.DrawEllipse(r, x, y, 1, 1);
                }  
            } //for koniec
            try { pictureBox1.Image = bmp; }
            catch { }
            pictureBox1.Invalidate();
            g.Dispose();
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
       bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);  
    }

}

0

napisz jak odpalasz ten watek i jaki blad masz bo sadze ze tutaj ci chodzi o odwolania miedzy watkami...wiec sprobuj najpierw wrzucic cos takiego :
System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;
jak nie pomoze to zapodaj dokladnie co sie dzieje.
pozdr

0

wiec dostaje gdy odpale program to narysuje troszeczke i dostaje takiego errora:
Obiekt jest obecnie używany gdzie indziej(InvalidOperationException was unhandled)
przy linijce z g = Graphics.FromImage(bmp) lub Application.Run(new Form1());

Wstawie cały kod to może bedzie bardziej wiadomo ocb

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace neuron1
{

public partial class Form1 : Form
{
    Bitmap bmp;
    public Thread Rysuj;
    public Tneuron n1 = new Tneuron(3);
    public Tneuron n2 = new Tneuron(3);
    public Tneuron n3 = new Tneuron(3);
    public Tneuron n4 = new Tneuron(3);
    public Tneuron n5 = new Tneuron(3);
    public Tneuron n6 = new Tneuron(6);
    public Graphics g;
    
    public Form1()
    {
        InitializeComponent();
        System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;
    }
    
    void watek()
    {
        double[] wejscia1 = new double[3];
        double[] wejscia2 = new double[6];
        
        wejscia1[0] = 1;
        wejscia2[0] = 1;

        int x;
        int y;
        Random losuj = new Random();
        
        double odpowiedz;
       
        while (true)
        {
            g = Graphics.FromImage(bmp);
              for (int i = 0; i < 100; i++)
              {
                //wejscie
                x = losuj.Next(513);
                y = losuj.Next(513);
                wejscia1[1] = ((double)x - 256 / 256);
                wejscia1[2] = ((double)y - 256 / 256);
                wejscia1[2] = 1 - wejscia1[2];
                //warstwa 1

                if (radioFunkcjaA.Checked)
                {
                    wejscia2[1] = n1.ObliczWyjscie(wejscia1, 'A');
                    wejscia2[2] = n2.ObliczWyjscie(wejscia1, 'A');
                    wejscia2[3] = n3.ObliczWyjscie(wejscia1, 'A');
                    wejscia2[4] = n4.ObliczWyjscie(wejscia1, 'A');
                    wejscia2[5] = n5.ObliczWyjscie(wejscia1, 'A');
                    //wyjscie
                    odpowiedz = n6.ObliczWyjscie(wejscia2, 'A');
                }
                else
                {
                    wejscia2[1] = n1.ObliczWyjscie(wejscia1, 'F');
                    wejscia2[2] = n2.ObliczWyjscie(wejscia1, 'F');
                    wejscia2[3] = n3.ObliczWyjscie(wejscia1, 'F');
                    wejscia2[4] = n4.ObliczWyjscie(wejscia1, 'F');
                    wejscia2[5] = n5.ObliczWyjscie(wejscia1, 'F');
                    //wyjscie
                    odpowiedz = n6.ObliczWyjscie(wejscia2, 'F');
                }

                //rysowanie
                
                Pen z = new Pen(Color.Green, 1.0f);
                Pen r = new Pen(Color.Red, 1.0f);
                if (odpowiedz > 0)
                {
                    g.DrawEllipse(z, x, y, 1, 1);
                }
                else
                {
                    g.DrawEllipse(r, x, y, 1, 1);
                }  
              } //for koniec

            try { pictureBox1.Image = bmp; }
            catch { }

            pictureBox1.Invalidate();
            g.Dispose();
        }
       
    }
    
    private void buttonStart_Click(object sender, EventArgs e)
    {
        Thread Rysuj = new Thread(new ThreadStart(watek));
        Rysuj.Priority = ThreadPriority.Normal;
        Rysuj.IsBackground = true;
        Rysuj.Start();
        
        buttonStart.Enabled = false;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
       bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
       
    }

    private void button_zamknij_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 forma2 = new Form2(this);
        forma2.Show(); 
    }



}

}

0

tak wydlada blad

System.InvalidOperationException was unhandled
Message="Obiekt jest obecnie używany gdzie indziej."
Source="System.Drawing"
StackTrace:
w System.Drawing.Image.get_Width()
w System.Drawing.Image.get_Size()
w System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
w System.Windows.Forms.PictureBox.get_ImageRectangle()
w System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
w System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
w System.Windows.Forms.Control.WmPaint(Message& m)
w System.Windows.Forms.Control.WndProc(Message& m)
w System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
w System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
w System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
w System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
w System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
w System.Windows.Forms.Application.Run(Form mainForm)
w neuron1.Program.Main() w C:\Users\Daniel\Desktop\neuron1\neuron1\neuron1\Program.cs:wiersz 18
w System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
w System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
w System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
w System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
w System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
w System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
w System.Threading.ThreadHelper.ThreadStart()
InnerException:

lub tak

System.InvalidOperationException was unhandled
Message="Obiekt jest obecnie używany gdzie indziej."
Source="System.Drawing"
StackTrace:
w System.Drawing.Graphics.FromImage(Image image)
w neuron1.Form1.watek() w C:\Users\Daniel\Desktop\neuron1\neuron1\neuron1\Form1.cs:wiersz 49
w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
w System.Threading.ThreadHelper.ThreadStart()
InnerException:

co jest zle

1 użytkowników online, w tym zalogowanych: 0, gości: 1