【问题标题】:call c# from vb.net is hanging从 vb.net 调用 c# 挂起
【发布时间】:2020-10-04 09:50:27
【问题描述】:

ina c# 项目我有一个静态异步任务,用于通过 tmpt-relay 发送电子邮件。 主要功能正在调用此任务。 这工作正常。 我的问题是我从 vb.net 项目中调用此函数,这也有效,电子邮件已发送但 vb.net 挂在 c#-call 中

我的 c# 代码

public class SmoffMail
    {
        public static string sRet;

        public string sendMail(string mailto)
        {
            RunAsync(mailto).Wait();
            return sRet;
        }

       static async Task RunAsync(string mailto){

            MailjetClient client = new MailjetClient("419cce0b9807d1016642156966fc4ec1", 
            ....    
        
            sRet = "ok.....";
        }
    }

从 vb.net 我这样称呼它:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim smail As New smoff.Mailjet.SmoffMail
        Dim i As Integer = 0
        Dim sRet As String = ""

        sRet = smail.sendMail("xxxxxxxh@bluewin.ch")

        MsgBox(sRet)

    End Sub
End Class

所以函数 smail.sendMail() 被调用但挂起,代码中的下一行 ( msgbox ) 是神经质的尝试。

感谢您的帮助

【问题讨论】:

标签: c# vb.net function cal


【解决方案1】:

您的 C# 应如下所示:

public class SmoffMail
{

   static async Task<string> SendMailAsync(string mailto){

        MailjetClient client = new MailjetClient("419cce0b9807d1016642156966fc4ec1", 
        ....    
    
        return "ok.....";
    }
}

你的 VB 应该是这样的:

Public Class Form1
    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim sRet As String = ""

        sRet = Await smoff.Mailjet.SmoffMail.SendMailAsync("xxxxxxxh@bluewin.ch")

        MsgBox(sRet)

    End Sub
End Class

让您的 VB 在等待上传 2 GB 附件的同时返回绘制 UI,否则将这些都设为异步是没有意义的

【讨论】:

    【解决方案2】:

    我找到了解决方案,问题不是我的代码,而是 API Mailjet.client。 更改对此 API 的调用后,它工作正常

    【讨论】:

    • 怎么改?如果您真正具体展示更改后的代码是什么,这对未来的读者会更有用
    猜你喜欢
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多