KoenZomers/UniFiApi

C# windows form application sample

Closed this issue · 4 comments

Actually this is not a issue. but i need a sample of windows form application. in that form i need to click button for block user. i have tried it through console app. it works grate. i need use it to windows form app too. can you send me sample

Hi @tiransa, what's the part you get stuck on with a forms application? Code-wise it will be the same as your console, but instead you just execute the command behind a button click.

i have tried and now it is working but there is small thing that not end await command then program stuck. i use kill current process for now.
`
string mac = textBox1.Text;
string url = Properties.Settings.Default.url;
string user = Properties.Settings.Default.user;
string password = Properties.Settings.Default.password;
string site = Properties.Settings.Default.site;
using (var uniFiApi = new Api(new Uri(url)))
{

            uniFiApi.DisableSslValidation();
            var authenticationSuccessful = await uniFiApi.Authenticate(user, password);

            if (!authenticationSuccessful)
            {
                Console.WriteLine("- Authentication failed");
                return;
            }
            Console.WriteLine("- Authentication success");
            var confirmResult = MessageBox.Show("Are you sure to Block this Device ??",
                                 "Confirm Block!!",
                                 MessageBoxButtons.YesNo);
            if (confirmResult == DialogResult.Yes)
            {
             await uniFiApi.BlockClient(mac);
                
                
                Process.GetCurrentProcess().Kill();
               
            }
            else
            {
                Process.GetCurrentProcess().Kill();
            }`

@tiransa, I just created a sample Windows Forms application to demonstrate how the API can be used and ran into the same process freeze as you describe. It's because of the async operation in the Dispose method. I've fixed it in the code and NuGet package. I've added the Windows Forms sample to the existing solution, so you can have a look at it how to get it to work, which is basically how you are using it already. You should be able to get rid of the process kill with the latest version I have just published.

yes. i feel now it is ok. working properly. thanks