I have a very simple problem, how can I run this function after click the button:

private void button1_Click(object sender, EventArgs e)
{
               my_function();
}

public void my_function()
{
              while(1){
              //////something
              richTextBox1.Text += "something.....";
              if(a==b)paint_something(val1,val2);
              ///something
              }
}

I wanna draw something and write to textbox in the same time?

thx 4 help

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

while(1) never works in C#! It works in C and C++, but then again, C# is a different brand and more correct here. 1 is an integer, a while should test a boolean condition, not an integer. So while (x==1) or while (true) is the only way to do it in C#.

doesn't matter i can be f.e. for(i=0;i<100;i++) ;pp I just wanna know how can I draw and write in the same time;)

If you're a male you can't, only women seem to have that ability;)
A computer can only simulate 2 or more processes happening at the "same" time. Because a computer is "quick" and even more if it is a multiprocessor.

Comments
LOL

If you're updating a GUI then that must take place on the main thread. I think this example is something like you're wanting to do:

using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmDoubleDraw : Form
  {
    public frmDoubleDraw()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      new Action(DrawBox1).BeginInvoke(
        new AsyncCallback(ThreadCallback),
        null);
      new Action(DrawBox2).BeginInvoke(
        new AsyncCallback(ThreadCallback),
        null);
    }

    private void DrawBox1()
    {
      for (int i1 = 0; i1 < 500; i1++)
      {
        //We need to invoke this to the main thread from the thread pool
        this.Invoke(new MethodInvoker(
          delegate()
          {
            richTextBox1.Text += i1.ToString() + Environment.NewLine;
            richTextBox1.Select(richTextBox1.Text.Length, 0);
            richTextBox1.ScrollToCaret();
          }));
        System.Threading.Thread.Sleep(10); //add a little delay
      }
    }
    private void DrawBox2()
    {
      for (int i1 = 500; i1 < 1000; i1++)
      {
        this.Invoke(new MethodInvoker(
          delegate()
          {
            richTextBox2.Text += i1.ToString() + Environment.NewLine;
            richTextBox2.Select(richTextBox2.Text.Length, 0);
            richTextBox2.ScrollToCaret();
          }));
        System.Threading.Thread.Sleep(10); //add a little delay
      }
    }

    private void ThreadCallback(IAsyncResult ar)
    {
      AsyncResult result = (AsyncResult)ar;
      Action act = (Action)result.AsyncDelegate;
      try
      {
        //This will catch exceptions from DrawBox1() and DrawBox2()
        act.EndInvoke(ar);
      }
      catch (ThreadAbortException)
      {
        //Application is closing. Not a big deal.
#if (DEBUG)
        System.Diagnostics.Debugger.Break();
#endif
      }
      finally
      {
        //Any drawing cleaningup
      }
    }


  }
}

That form has a button and 2 rich text boxes. I copy/pasted the code but you could switch one of the methods out to implement your desired drawing functionality.

Edited 6 Years Ago by sknake: n/a

This article has been dead for over six months. Start a new discussion instead.