call c# from vb.net is hanging

99 views Asked by At

ina c# project i have a static async task written to send emails via tmpt-relay. the main function is calling this task. This is working fine. My Problem is i'm calling this function from a vb.net project, this is also working, email was send but vb.net is hanging in the c#-call

My Code in 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.....";
        }
    }

From the vb.net i call it like this:

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("[email protected]")

        MsgBox(sRet)

    End Sub
End Class

So the function smail.sendMail() was called but is hanging, the next line in code ( msgbox ) is nerver attempt.

Thanks for your help

2

There are 2 answers

1
user3387175 On

i found the solution, the problem was not my code, it was the API Mailjet.client. after changing the call to this API it works fine

0
Caius Jard On

Your C# should look like this:

public class SmoffMail
{

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

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

And your VB should look like this:

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("[email protected]")

        MsgBox(sRet)

    End Sub
End Class

Let your VB go back to drawing the UI while it waits for your 2 gigabyte attachment to upload, otherwise there's no point in having any of this be async