I have an application (WinForms). It simply grabs some data, does some work on it, then uploads the result to some cloud storage. I'm now using some third party code which handles the authentication with the cloud storage for me. This code will create a WinForm, which hosts a browser control, which pops up if the user has not already provided credentials and permission for the application to use the cloud storage. Before my program goes and does its thing, I want to make sure I'm already authenticated, hence in the Main() loop of the program (Program.cs), I want to use the third party library to authenticate with the cloud storage and block my actual code (//myCode) executing until it is done. 'Unfortunately' the library is asynchronous, and I've spent the best part of today trying to get the behavior I've just described - my question is effectively a "make async function synchronous", but the general answers I've seen around don't seem to work for me.
So I have something like this
static void Main(string[] args)
{
OAuth.LoginAuthAsync("clientID", "client_secret", ...)
//Don't want to get here until LoginAuthAsync is done
application.run(myForm); //myCode
}
But this will popup the web browser and run my code, which I want to happen only after the web OAuth.LoginAuthAsync has completed.
I'm no expert with the whole TPL libraries, but I've spent a while playing around with things I believe should work but don't. I've tried:
I've tried
Task.Run(async () =>
{
OAuth.LoginAuthAsync("123456", "asdasd", ...));
).Wait();
Which throws an error in the third party library
System.Threading.ThreadStateException: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment. at System.Windows.Forms.WebBrowserBase..ctor(String clsidString) at System.Windows.Forms.WebBrowser..ctor()
So then I tried
Thread tx = new Thread(() => OAuth.LoginAuthAsync("123456","asdasd", ……..));
tx.SetApartmentState(ApartmentState.STA);
tx.Start();
tx.Join();
but that just pops up the window and it disappears instantly, and //myCode executes.
Any body have any ideas what I can do here. I tried moving the method inside of myForm _Load event (that's myForm invoked from the application.run), but I have the same problem.
Many Thanks
P.S. I don't want to use any further third party libraries.