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 8954571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:20:23+00:00 2026-06-15T14:20:23+00:00

I am trying to make a thread class. The class simply creates a thread

  • 0

I am trying to make a thread class. The class simply creates a thread of the new form to open

class Threading
{
    private static int sendingForm;
    public void StartThread()
    {
        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
        t.SetApartmentState(System.Threading.ApartmentState.STA);
        t.Start();
    }

    public static void ThreadProc()
    {
        switch (sendingForm)
        {
            case 1:
                System.Windows.Forms.Application.Run(new MainForm());
                break;
            case 2:
                System.Windows.Forms.Application.Run(new ReportPicker());
                break;
        }
    }
}

The idea is simple, I am just practicing using threads. I don’t want to do it in each form, so I tried recycling a bit of code by making it in a class. As you can see, the way I detect the form I want to go is by sending a number depending on the form I want to go. Based on that number will be the form the thread runs. I want to better this if it’s possible. I was thinking of maybe a way to send as parameter the form that I want to go to, but since each form is it’s own type, I can’t find a way to do it. As a matter of fact, I don’t know if it’s possible to do. So, I ask here if you can help me make my code better. It doesn’t matter if it’s by the method I’m asking, just to recycle as much code as possible. This is for the sake of learning to use threads. Thanks to anyone who can 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-15T14:20:24+00:00Added an answer on June 15, 2026 at 2:20 pm

    Ok, you want to cut out the redundancy and duplication in ThreadProc. That’s good! Here is a possible solution:

    public static void ThreadProc<TForm>() where TForm : Form, new()
    {
        System.Windows.Forms.Application.Run(new TForm());
    }
    
    new System.Threading.Thread(() => ThreadProc<MainForm>())
    

    Or, reflection-based:

    public static void ThreadProc(Type formType)
    {
        System.Windows.Forms.Application.Run((Form)Activator.CreateInstance(formType));
    }
    
    new System.Threading.Thread(() => ThreadProc(typeof(MainForm)))
    

    Or, caller-injected:

    public static void ThreadProc(Form form)
    {
        System.Windows.Forms.Application.Run(form);
    }
    
    new System.Threading.Thread(() => ThreadProc(new MainForm()))
    

    Note, that the caller has to change in all cases to adopt the new calling convention.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just beginning to learn C++ and I'm trying to make Thread class that
I'm trying to make a simple webreader work in an AsyncTast, main thread public
am trying to make a worker thread using MFC so here is the codes:
There is a thread that fills the db and I am trying to make
I am fairly new to iOS development and trying make a simple app which
I am trying to make a simple server thread in QT to accept a
I'm a total n00b to c++ and am trying to make a simple form
Class Test{ int value; static void* thread_func(void* args){ value++; } void newthread(){ pthread_create(&thread_func,...); }
I'm trying make a login window where a user is prompted to enter their
I'm trying make an entity with doctrine that has three associations with other entities

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.