Click here to Skip to main content
15,887,476 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i created simple threading program. it runs without debug mode. but start debugging mode it does not run.the program will crash every time. how to correct this

What I have tried:

i googled my problem.and i cant find any solution.

Code Try:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;


namespace thread_test_2
{
  public partial class Form1 : Form
  {
    Thread t;
    public Form1()
    {
       InitializeComponent();
    }
    public void tw()
    {
      for (int i = 0; i < 10000; i++)
      {
        textBox1.Text = i.ToString();
      }
    }
    private void button1_Click(object sender, EventArgs e)
    {
      t = new Thread(tw);
      t.Start();
    }
  }
}
Posted
Updated 4-Feb-16 21:33pm
v4
Comments
dan!sh 5-Feb-16 0:09am    
What is the exception you are getting when you run with debug?
Member 12235586 5-Feb-16 0:23am    
Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
dan!sh 5-Feb-16 0:28am    
Are you accessing control from another thread?
Member 12235586 5-Feb-16 0:42am    
no. this is my full code. please check it

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


namespace thread_test_2
{
public partial class Form1 : Form
{
Thread t;
public Form1()
{
InitializeComponent();
}
public void tw()
{
for (int i = 0; i < 10000; i++)
{
textBox1.Text = i.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
t = new Thread(tw);
t.Start();
}
}
}
[no name] 5-Feb-16 0:19am    
And also share thread relevant code.

You are accessing the textbox in the thread you have created. You are doing this in your tw method. This is not allowed.

You are only getting this error while debugging because illegal cross thread calls are only checked if debugger is attached. Here is the relevant part of code from MSDN[^] (Line 300).

C#
// Initially check for illegal multithreading based on whether the
// debugger is attached.
[ResourceExposure(ResourceScope.Process)]
private static bool checkForIllegalCrossThreadCalls = Debugger.IsAttached;



You should take a look at BackgroundWorker. It may help you achieve what you are trying to do here.
 
Share this answer
 
v2
Comments
Member 12235586 5-Feb-16 1:16am    
could you can edit my code including Backgroundworker please??
dan!sh 5-Feb-16 1:35am    
It would be good if you do it yourself. You can read about MethodInvoker and BackGroundWorker and use what suits you better.
Member 12235586 5-Feb-16 1:37am    
ok. i'll study those and use them. thank you very much for your solution.
As an alternative to using a background worker you can use BeginInvoke to ensure the text is changed on the main user interface thread.

This CodeProject article gives a very good overview and has a code example doing almost exactly what you need to fix your sample code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900