I am trying to access C#.Net dll from python and print the status in python while executing C# methods. Please help me to resolve this. I tried below code:
C# dll class library contains windows form control
using System;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
namespace TestLib
{
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
}
public string Method()
{
try
{
Task task = Task.Factory.StartNew(() => Interact_With_Python());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return "Forms Says Hello";
}
private void Interact_With_Python()
{
PrintStatus("Hello World...");
Thread.Sleep(1000);
PrintStatus("Hello World...1");
Thread.Sleep(1000);
PrintStatus("Hello World...2");
Thread.Sleep(1000);
}
private delegate void UpdateStatusDelegate(string status);
//Print this Status in Python
private void PrintStatus(string status)
{
if (this.richTextBox.InvokeRequired)
{
this.Invoke(new UpdateStatusDelegate(this.PrintStatus), new object[] { status });
return;
}
this.richTextBox.AppendText(status);
}
}
}
Python: Calling methods from C# dll
import clr
from types import *
from System import Action
clr.AddReference("C:\..\TestLib.dll")
import TestLib
form = TestLib.TestForm()
form.Show()
val = form.Method()
print(val)