S
S
Sergey Karbivnichy2019-02-27 13:46:38
.NET
Sergey Karbivnichy, 2019-02-27 13:46:38

Accessing control from another thread?

There is a program on winforms. Double-clicking on the listview starts playing an internet radio station. But since the form freezes for a couple of seconds with each click, I decided to execute the Play method in another thread. The program works correctly, but if you run the program under a debugger, an "InvalidOperationException" exception appears.
5c7667a591ecc640744948.png
The Play method takes 2 parameters - a string with the stream url and a float volume. I take the Url from the database through the GetUrlFromDB method (I pass the name of the radio station from the listview as a parameter and get the url by name). I decided to modify the code a little and when I click on the listview, I first pass the url to the tmp variable of the string type, and I already pass the url from the tmp variable to the Play method. The studio stopped swearing and everything works as it should. Is it possible to do so? I just looked at the examples (MSDN, stackoverflow), so there you need to write a lot of code, you need to create several additional methods and delegates, which is a bit complicated for me at the moment.
Working code:
5c766a83836d3662658593.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kwayne, 2019-02-27
@hottabxp

Use the invoke method on lv.
lv.Invoke((MethodInvoker)delegate { lv.FocusedItem...; };
https://docs.microsoft.com/en-us/dotnet/api/system...

J
John_Nash, 2019-02-27
@John_Nash

I decided to modify the code a little and when I click on the listview, I first pass the url to the tmp variable of the string type, and I already pass the url from the tmp variable to the Play method. The studio stopped swearing and everything works as it should. Is it possible to do so?

for string you can

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question