1

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");
    }
C K
  • 23
  • 5
  • If it crashes then there is an Exception. Can you catch the Exception and edit your question to include the details? – Crowcoder Apr 29 '22 at 13:43
  • Wait, why are you recursively calling Main()? – Crowcoder Apr 29 '22 at 13:45
  • @Crowcoder thanks for your reply, I'm recursively calling Main() in order to loop the program in infinite (every 30sec thanks to await Task.Delay(30000) ), I will try to catch the exception but as I said I'm not sure the program is crashing, may be just ignoring the web request or stopping at the request without going further – C K Apr 29 '22 at 13:51
  • You need to `await` it – Crowcoder Apr 29 '22 at 13:53
  • @Crowcoder Await is already put behind the web client (var content = await client.GetStringAsync), but it seem the program is just stopping without going further, what to do in order to keep the await and manage the data gathered – C K Apr 29 '22 at 13:55
  • 1
    You need to await async calls or return the Task they have started and then await that. You can't only await a method that starts a Task. – Crowcoder Apr 29 '22 at 14:22
  • @Crowcoder thanks you helped me, my code is proper (I followed your cuncils and tried to catch exception, the problem was to not handling a SSL request) – C K Apr 29 '22 at 14:31

0 Answers0