Declaring Thread in a Constructor in C

0 votes

I'm learning about threads and how to work with them right now. Simultaneously, I'm honing my comprehension of events/global events (just for context).

I created a thread inside an object and gave it a function to utilise on thread.Start()

internal class Name
    {
        private Thread testthread;

        private EventWaitHandle globalEvent;

        private Eventstest evente = new Eventstest(); //Just to add some methods

        public Name(Thread testthread, EventWaitHandle globalEvent)
        {
            this.testthread = testthread;
            this.globalEvent = globalEvent;
        }

        
        public void Execute()
        {
            bool terminate = false;
            bool eventset = false;
            bool rdy = false;

            while (!terminate)
            {
                if (evente.CheckSysEvent(globalEvent)) 
                {
                    eventset = true; //This is just to check with debugger if the event was raised elsewhere
                }
                Thread.Sleep(100);
            }
        }
    }

So, in this example in a Windows Forms App, I'm attempting to create an instance of this class while also creating an instance of the thread (with the work method the should run later on). This is where I'm having trouble.

private void btn_runThread_Click(object sender, EventArgs e)
        {
            threadClass = new Name(new Thread(ProblemHere), globalEvent);
            threadClass.Execute();            
        }

This is a button that starts the thread working on the task at hand. In forms1.cs, the variable threadClass is simply the initialization:

Name threadClass;

I understand that it requires a delegate to pass the thread's start method. I tried everything I could think of and couldn't get it to work. I can't just call the method and expect it to work. And, as far as I can tell, the material I found in the c# documentation is basically just passing the function. Which is most likely incorrect. And, as I just realised, if the property/thread.start is only created at runtime, how am I able to use it?

Jun 11, 2022 in C# by pranav
• 2,590 points
278 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C#

0 votes
1 answer

Encapsulation of subclasses from a constructor in C#

If you think about it in terms ...READ MORE

answered Jun 11, 2022 in C# by pranav
• 2,590 points
406 views
0 votes
1 answer

Is a Library a project or a C# source code file in Visual Studio?

A library project is a collection of ...READ MORE

answered Jun 7, 2022 in C# by rajiv
• 1,620 points
298 views
0 votes
1 answer

How do I turn a C# object into a JSON string in .NET

Newtonsoft.json can be used to accomplish this. ...READ MORE

answered Jun 13, 2022 in C# by krishna
• 2,820 points
407 views
0 votes
1 answer

How to find the extension of a file in C#

Go to here https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getextension?redirectedfrom=MSDN&view=net-6.0#System_IO_Path_GetExtension_System_String_ string myFilePath = @"C:\MyFile.txt"; string ext ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
379 views
0 votes
0 answers

Learning C from a background in C#

I'd like to increase my programming expertise, ...READ MORE

Jun 11, 2022 in C# by jyoti
• 1,240 points
196 views
0 votes
0 answers

Creating a generic method in C#

I'm attempting to integrate several comparable techniques ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
344 views
0 votes
0 answers

C# Threading in real-world apps

Learning about threading is indeed fascinating, and ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
269 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,140 points
5,300 views
0 votes
1 answer

What is the difference between Runnable and extends Thread

Runnable is the preferred way to create ...READ MORE

answered May 1, 2018 in Java by sharth
• 3,370 points
934 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP