如何在Python中打印C#.Net回调函数
我试图从python中访问C#.Net dll,并在执行C#方法时用python打印状态。请帮我解决这个问题。 我尝试了以下代码: C#dll类库包含windows窗体控件如何在Python中打印C#.Net回调函数,c#,python,C#,Python,我试图从python中访问C#.Net dll,并在执行C#方法时用python打印状态。请帮我解决这个问题。 我尝试了以下代码: C#dll类库包含windows窗体控件 using System; using System.Windows.Forms; using System.Threading; using System.Threading.Tasks; namespace TestLib { public partial class TestForm : Form {
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:从C#dll调用方法
最后,我从stackoverflow中找到了解决方案 C#Net Python代码:
import clr
from types import *
from System import Action
clr.AddReference("Path\TestLib.dll")
import TestLib
print("Hello")
frm = TestLib.TestForm()
fakeparam="hello"
def f(response):
print response
val = frm.Interact_With_Python(Action[str](f))
print val
输出:
Hello
Executing Step1
Executing Step2
Executing Step3
Done
你的错误是什么?我没有收到这个代码的任何错误。它只打印“Forms Says Hello”,但我希望输出是[“Hello World…”、“Hello World…1”、“Hello World…2”、“Forms Says Hello”]。这是ironpython吗?
import clr
from types import *
from System import Action
clr.AddReference("Path\TestLib.dll")
import TestLib
print("Hello")
frm = TestLib.TestForm()
fakeparam="hello"
def f(response):
print response
val = frm.Interact_With_Python(Action[str](f))
print val
Hello
Executing Step1
Executing Step2
Executing Step3
Done