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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:39:56+00:00 2026-05-16T01:39:56+00:00

So I made a Winforms GUI in Visual Studio using C#, but for the

  • 0

So I made a Winforms GUI in Visual Studio using C#, but for the project I am working on I want the majority of the code to be written in Python. I am hoping to have the “engine” written in python (for portability), and then have the application interface be something I can swap out.

I made the C# project compile to a .dll, and was able to import the classes into an IronPython script and start the GUI fine.

The problem is that running the GUI stops the execution of the Python script unless I put it into a separate thread. However, if I put the GUI into a separate thread and try and use the original python thread to change state information, I get an exception about modifying a control from a different thread than what created it.

Is there any good way to communicate with the GUI thread or a way to accomplish what I am trying to do?

The C# driver of the GUI:

public class Program
{
    private static MainWindow window;

    [STAThread]
    static void Main()
    {
        Program.RunGUI();
    }

    public static void RunGUI()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        window = new MainWindow();
        Application.Run(window);
    }

    public static void SetState(GameState state)
    {
        window.State = state;
    }
}

And the python script:

import clr
clr.AddReferenceToFile("TG.Model.dll")
clr.AddReferenceToFile("TG.UI.dll")
from TG.Model import GameState
from TG.UI import Program
import thread
import time


def main():
    print "Hello!"
    state = GameState()
    print state.CharacterName
    print dir(Program)
    thread.start_new_thread(Program.RunGUI, ())
    #Program.RunGUI()
    time.sleep(2)
    Program.SetState(state)
    raw_input()


if __name__ == "__main__":
    main()
  • 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-05-16T01:39:57+00:00Added an answer on May 16, 2026 at 1:39 am

    Put everything after the call to Program.RunGUI() in an event handler.

    C#:

    public static void RunGUI(EventHandler onLoad)
    {   
        ...
    
        window = new MainWindow();
        window.Load += onLoad;
        Application.Run(window);
        window.Load -= onLoad; //removes handler in case RunGUI() is called again
    }
    

    Python:

    def onload(sender, args):
        time.sleep(2)
        Program.SetState(state)
        raw_input()  
    
    def main():
        ...
        Program.RunGUI(onload)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a VERY simple C# Winforms project that I made using Visual Studio
I made a project in Visual Studio 2010 (winforms). I made a new Form
I've been looking for a Visual Studio-like docking library for WinForms, but the two
All, I'm using MSVS 2010 doing a project with C#/WinForms. I made an object
I've made a simple C# WinForms app, which makes a screen-capture using System; using
I have a Winforms application which is working fine.. using a BackgroundWorkerThread to manage
I have made a deployment project for my very simple .Net 4.0 C# Winforms
Possible Duplicate: WPF versus Winforms I have been using visual C# for a while
Using Winforms, is there a way to allow resize, but only to allow the
Here is a synopsis of my code which is a moderately complex WinForms GUI.

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.