Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8608157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:32:16+00:00 2026-06-12T03:32:16+00:00

Im having a quite annoying problem with threading in C#. I get the error

  • 0

Im having a quite annoying problem with threading in C#. I get the error “A field initializer cannot reference the non-static field, method, or property ‘Scraper.Form1.scrapeStart()'” When using this code:

public partial class Form1 : Form
{
    public Thread scrape = new Thread(() => scrapeStart()); //This is where the error happens
    public About about = new About();
    public Form1()
    {
        InitializeComponent();
    }

    public void appendOutput(String s)
    {
        output.AppendText(s);
        output.SelectionStart = output.Text.Length;
        output.ScrollToCaret();
        output.Refresh();
    }

    public void scrapeStart(){
        Button button1 = new Button();
        appendOutput("");
        button1.Enabled = true;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        about.ShowDialog();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        scrape.Start();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        scrape.Abort();
        button1.Enabled = true;
    }
}

I realize that if I made the function scrapeStart static it would work, but that would make appendOutput(“”); and button1.Enabled = true throw errors. And if I put the new Thread in where its started (button1_Click) then it cannot be aborted in button2_Click.

Im a bit knew to C# so I may have either done everything horribly wrong, or it may just be a small problem. But either way, could someone please help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:32:17+00:00Added an answer on June 12, 2026 at 3:32 am

    This really has nothing to do with threading. You’ll see exactly the same problem if you write:

    public class Foo
    {
        int x = 10;
        int y = x;   
    }
    

    Or even more clearly:

    public class Bar
    {
        object obj = this;
    }
    

    There’s the slight distraction here that the this reference is implicit – you’re creating a delegate with a target of this.

    The solution is just to put the assignment into the constructor:

    public Thread scrape;
    public About about = new About();
    public Form1()
    {
        InitializeComponent();
        scrape = new Thread(scrapeStart);
    }
    

    As an aside:

    • Please don’t use public fields!
    • Please adhere to .NET naming conventions
    • You’ll also need to fix the threading parts of scrapeStart, which shouldn’t be accessing UI elements directly
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a quite annoying problem. I want to get the current date/time
I am having quite a tricky problem, which i just cannot seem to solve.
I'm having a quite annoying problem with iOS 6.0. Everytime the IUDevice orientation changes
I'm quite new to OpenId and I'm having a bit of a problem understanding
I'm having quite a weird problem here. I have just pasted the Yii folder
I'm having quite the problem now. It's only bothering in Firefox (Testing with FF10),
I'm having quite a frustrating problem that I'm sure is TRIVIAL. Whenever I move
I'm having a quite strange problem with the fwd_iterator that I'm implementing: if I
I'm having quite a bit of problem deciding how to represent a certain problem.
I'm having quite a peculiar problem with PhoneGap and JQuery Mobile, using the latest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.