0

I'm still learning how to make my own async methods, I've been using the common async ef methods, but this time I have to use a third party dll, but the main method isn't async. This how it simple works.

        thirdpartymethod tMethod = new thirdpartymethod();
        tMethod.data1 = "test";
        tMethod.data2 = 5;
        string tResult = tMethod.GenerateSignature();

That method, consumes a webservice and generate a transaction file on drive C.

I tried something like this:

string tResult = await Task.Run(() => tMethod.GenerateSignature());

And It kind of works, because the screen doesn't lock, but I get an error that the file is in use (That's the transaction file that the thirdparty method creates).

I've been searching, but most people want run async method sync.

Thank you for your response.

Edit:

It only happens when I tried to call it async, I used this third party dll for about a month with no problem, but it locks the screen if the user have a bad internet connection.

I know what a file in use is, but it just happend when I did the change, I can't change the thirdparty file.

My question got flaged by an Admin, so I'll keep looking somewhere else. Thank you to everybody that took the time to answer.

Edit:

The error is:

The process cannot access the file 'C:\SIGNATURES\signature_response.json' because it is being used by another process.

I found that they have a boolean option to avoid the use of the transaction file, so I turned it off and now it works. Another thing I noticed is that they use an absolute path so if there is not drive C or no Admin privileges will throw an error. (I checked that with an IL decompiler). So I'm not going to use that automatic transaction file, Ill do it my way becuase the information on the transacion file is part of the result string that I receive.

Thank you all.

menendeze
  • 1
  • 3
  • 1
    So? Whats the problem with the `Task.Run()`? The exception seems to be non-sync related. – Jeroen van Langen May 03 '20 at 21:53
  • 1
    Does the exception go away when you call the third party API method synchronously? Since you await the execution of GenerateSignature(), it is all the same from the third party method's perspective. – Oguz Ozgul May 03 '20 at 21:55
  • I think you should implement some [synchronization](https://learn.microsoft.com/en-us/dotnet/standard/threading/overview-of-synchronization-primitives) cause the third party lib(or your usage of it) is not threadsafe. – Guru Stron May 03 '20 at 22:11
  • _"I get an error that the file is in use"_ -- that's because it is. See marked duplicate. If you would like more specific advice than the extensive information found in the thirteen different answers to that question, please post a new question, making sure you improve upon this one by: providing the _exact_ information about any error you get; explaining what research you've already done and why that hasn't answered your question; and, providing a good [mcve] that reliably reproduces the problem. – Peter Duniho May 03 '20 at 22:51
  • The exception only happens when I try to call it async. I've been using it for like a moth, but I don't want it to lock the user computer. So when I try to call it async with the information I got from the internet, For some reason it locks the file when it's not even done. – menendeze May 04 '20 at 03:15
  • Could you show the line where you get the error that the file is in use? – Theodor Zoulias May 04 '20 at 04:19

0 Answers0