我有一个控制台应用程序,它可以调用一个类并作为控制台应用程序运行或触发一个Windows窗体。 windows表单inturn发送参数并调用相同的操作。
调用点:
static void Main(string[] args)
{
if(AppSettingsHelper.GetValue<bool>("EnableWindowsForm"))
{
System.Console.WriteLine("EnableWindowsForm is set to true - Running Windows form");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1(0));
//First Time
var form = new ReportGeneratorForm();
Application.Run(form);
}
else
{
System.Console.WriteLine("EnableWindowsForm is set to false - Running direct program in console");
PortalMonitoring monitoring = new PortalMonitoring();
monitoring.Process();
}
}
现在,在单击按钮时,将触发相同的类
private void button1_Click(object sender, EventArgs e)
{
PortalMonitoring monitoring = new PortalMonitoring();
monitoring.Process(DateTime.Now); //Date as paramater- Default is null
}
如果我触发控制台应用程序,它运作良好。但是,如果我单击该按钮,代码将停留在async web api call -below代码点
int reportID = GetReportIDAsync().Result;
private static async System.Threading.Tasks.Task<int> GetReportIDAsync()
{
var reportName = "Portal name";
var reportID = await ops.GetReportId(reportName);
LogAndWriteToConsole("Report ID Feched : " + reportID.ToString());
return reportID;
}
请帮助我,我认为Windows窗体似乎不允许多线程。如何解决这个问题?
你没有显示从monitoring.Process()
到GetReportIDAsync()
的完整路径,但它需要异步/等待一直。
顶级应如下所示:
private async void button1_Click(object sender, EventArgs e)
{
// probably add a try/catch here
PortalMonitoring monitoring = new PortalMonitoring();
await monitoring.Process(DateTime.Now); //Date as paramater- Default is null
}
你的no-winforms分支然后使用monitoring.Process().Wait()