How to make a web request without crashing app and explorer

108 views Asked by At

I have tried many many ways since hours to make a web request in my app based on github.com/dsafa/CSDeskBand#winforms and specially on github.com/20154530/DeskBandTest which is an implementation.

By different ways of code, async or not, I tried to do a web request to get JSON and work with it but my app seem to crash (because the next Messagebox didn't show). I couldn't catch the JSON I want, either nothing happen either the app seem to stop/crash.

My question is : do you know a way/ the proper way to make a web request to catch the JSON without the app to stop/crash ? Big thanks for your help/reading in advance

Here is my code :

namespace BandTest_WinForm_ {
[ComVisible(true)]
[Guid("5731FC61-8530-404C-86C1-86CCB8738D06")]
[BandRegistration(Name = "app", ShowDeskBand = true)]
public partial class BandControlT : WinBandControl {

    public BandControlT() {
        InitializeComponent();
        _uidispatcher = Dispatcher.CurrentDispatcher;
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        // starting the app
        Main();
    }

    public async Task Main()
    {
        await Task.Delay(30000); // just a loop to execute every 30sec

        await CheckInternet(); // a task to check if domain to join is UP

        System.Windows.Forms.MessageBox.Show("getting ready to call request api", "h");

        await RequestAPI(); // call the task to get the JSON

        System.Windows.Forms.MessageBox.Show("get back to main", "h");
        Main(); // restart to the top
    }

    public async Task RequestAPI()
    {
        System.Windows.Forms.MessageBox.Show("entering to requestapi", "h");

        var client = new HttpClient();
        var content = await client.GetStringAsync("JSON API");

        System.Windows.Forms.MessageBox.Show("we are going to show content", "h");

        System.Windows.Forms.MessageBox.Show(content.ToString(), "h");
    }
0

There are 0 answers