Priscilla Jobin
Priscilla Jobin

Reputation: 607

How can I set a value to a label through a Thread?

This code is giving an error 'Object reference not set to an instance of an object'.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        _Default obj = new _Default();
        Thread thread = new Thread(new ThreadStart(obj.ThreadStart));
        thread.Start();

    }
    public void ThreadStart()
    {
        show();
    }
    private void show()
    {
        lblget.Text = "hi";
    }

Upvotes: 0

Views: 91

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94635

No need to create an instance of _Default.

protected void Page_Load(object sender, EventArgs e)
    {
        Thread thread = new Thread(show);
        thread.Start();
    }
    private void show()
    {
        lblget.Text = "hi";
    }

Read these article - Use Threads and Build Asynchronous Handlers in Your Server-Side Web Code and PageAsyncTask.

Upvotes: 1

Related Questions