Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calls requiring STA threads don't work #26

Open
rekna1 opened this issue Apr 24, 2019 · 5 comments
Open

Calls requiring STA threads don't work #26

rekna1 opened this issue Apr 24, 2019 · 5 comments

Comments

@rekna1
Copy link

rekna1 commented Apr 24, 2019

I have a script that need to access the clipboard. After compiling with lpless when I try to run executable it complains that to access clipboard it need STAthread. When I add this attribute to the linqpad script I get an error from compiler that it finds multiple entry points main and I need to specify main entry point. Any idea ?

@atifaziz
Copy link
Owner

Do you have a minimal LINQPad script that you can share (even if via LINQPad's Instant Share feature) along with steps that can help to reproduce the problem?

@rekna1
Copy link
Author

rekna1 commented Apr 25, 2019

Uses System.Windows.Clipboard

void Main()
{
	var text = Clipboard.GetText();
	Console.WriteLine(text);

}

The above compiles but gives the error on running:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made.

This was the solution I used before:

[STAThread]
static void Main()
{
	var text = Clipboard.GetText();
	Console.WriteLine(text);

}

But compiling gives

 Member 'UserQuery.Main()' cannot be accessed with an instance reference; qualify it with a type name instead
    testrun.cs(24,16): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.

@atifaziz atifaziz changed the title Compilation error with stathread attribute Compilation error with STAThread attribute Apr 30, 2019
@atifaziz atifaziz changed the title Compilation error with STAThread attribute Calls requiring STA threads don't work Apr 30, 2019
@atifaziz
Copy link
Owner

@rekna1 Thanks for sharing that example. I managed the reproduce the issue.

Meanwhile, a workaround would be to explicitly configure a thread for STA and run your code under that, like this:

void Run()
{
    var text = Clipboard.GetText();
    Console.WriteLine(text);
}

void Main()
{
    var t = new Thread(Run);
    t.SetApartmentState(ApartmentState.STA);
    t.Start();
    t.Join();
}

I've also shared the above query that you can download and try in LINQPad as well as run with LINQPadless.

@rekna1
Copy link
Author

rekna1 commented May 9, 2019

Oké, finally had a chance to try your workaround and that works great !

@atifaziz
Copy link
Owner

atifaziz commented May 9, 2019

Oké, finally had a chance to try your workaround and that works great !

Good to know and thanks for taking the time to drop in a line to let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants