I'm trying to interact with a third party application but whenever I try and call a method, I get an error message saying System.InvalidOperationException: Operation must be performed on the application thread. The exception is of type System.Reflection.TargetInvocationException and I'm guessing this is because my app is running in a completely separate process. Is there a way to get my program, which is a console application, to start running on the same thread as the third party app?
You can't get two separate applications to run in a single thread.
If you are trying to interact with some UI controls in your application, you can use .Invoke() to make sure the relevant code is executed in the UI thread.
If you're trying to manipulate the a third party application, the appropriate solution depends entirely on how you're interacting with it. Does it provide an API? Sending messages to its window handle? ??
In order to interact with a running instance of another application, you'll need to use something like remoting (obsolete) or WCF. Essentially, the other application acts as a server, and you have to somehow connect to it. Referencing its assembly, instantiating one of the classes therein defined and calling methods on that instance won't let you talk to an already-executing process.
Related
I try to develop an extension for Microsoft Edge based on native messaging and the official guide provides the example. And there is synchronization of access to the dictionaries of AppServiceConnections and their Deferrals in the OnBackgroundActivated method, but there is no such a thing in other event handling methods...
So my question is about UWP App Service threading model. Is it guaranteed that only one event handling method can be performed at a time? Or should I provide a correct synchronization of access to my data?
Is AppServiceConnection thread safe? Can I use SendMessageAsync from different threads at the same time? Or should I synchronize its usage?
I guess your issue is that you didn't see lock keyword inside events like OnAppServiceRequestReceived, OnAppServicesCanceled and so on, which is to do thread synchronization, and you're not sure if you should do this by yourself.
I think the answer should be no.lock inside OnBackgroundActivated is ensured to set correct desktopBridgeConnectionIndex or connectionIndex. Without the keyword lock inside these event handles not means that the event handle must be triggered only one time at a time. For one app service, if client A is connecting the app service, at the same time, another client B asks for the same app service, for this scenario the app service will spin up another instance of the same background task. So that for client A, its app service connection there is no side effect on client App B. In another words, each app service connection has its own instance, messages sending based on one app service connection have no influence with others. You may reference this video to look more details about app service, app service relative is about starting from 25th minute.
If you check the code snippet inside the event, you may see there are code lines to judge the request is from which app service connection, for example this.desktopBridgeConnection = desktopBridgeConnections[this.currentConnectionIndex].You will send message to correct AppServiceConnection, and this should be thread safe. If you met actual thread save issue when performing this, you could ask issue with testing details.
In my solution I have three projects: Application, Class Library providing data and Windows Runtime Component for background task. Problem is, I need bot application and background task to use data provider. And this ends with "The application called an interface that was marshalled for a different thread". Dispatcher isn't quite a solution as I can't retrun something while in dispatcher.
Background Task is called rarely, so is it possible to unmarshall data providing interface for a second so background task could get its data? If it is - how to do that, if it isn't - what else can I do?
You have two options:
Ensure that calls from the background thread are marshalled to the appropriate thread using a wrapper class. Typically this will mean writing a class that takes the original object as a parameter, replicates its class interface in it's own structure but checks for access before calling the methods directly.
Create a new data adaptor that can be called on the background thread.
Either of these options will solve your issue.
I have a C# winforms project which has a reference to a library also coming from myself. The winforms app triggers some work to do for the library. Is it possible that the library can finish its work even if the winforms app gets closed?
There are two possible approaches:
create a separate subprocess. Ending the parent process will not end the child, thus, the newly created task will continue when the parent app is closed
make the parent app in such way that closing the main form doesn't end the application, thus, giving the application time to end all worker threads spawned during its lifetime
I am not sure which of the two I would recommend, both seem risky as chances are the long-running background operation will not finish in reasonable time. And then what?
Is it possible that the library can finish its work even if the
winforms app gets closed?
No, not if that library is simply hosted in the main process.
You would need to do something along the lines of create a windows (or web) service and host the library in the service along with a message passing mechanism. Then call into it by having your windows application call a command on the service.
I've got a few instances of the same class. During the classes lifetime, every method call on this class should be executed on the same thread. But for each instance I need a different thread.
I thought about Threadpool, but it seems that I have too less control about it.
How can I reuse a thread without using ThredPool?
Thank you! Martin
Edit (why I need this):
I have to use a win32 dll to access business logic of a third-party product. This dll is not designed for a multi-threaded environment like a web application. When I run my ASP.NET MVC application in ASP Classic Mode (STA Thread), everything works fine so far. But the problem is that all users going to block each other. This component also maintains some state. As soon as a different thread is accessing this component, it will not recognize the connection-handle I have to pass in for each method call. I got the connection handle after a logon procedure. I want to put my web application in MTA mode back again and use a worker-concept, assigning about 10 users to a worker (max. 10 users should block each other). One worker should always use the same thread to execute the api calls so the component will not stubmle.
I'm not happy with this situation, but I have to find am acceptable solution.
Update - Found a Solution:
Thanks to the "Smart Thread Pool" from Ami Bar I could accomplish the behavior I was looking for (easily). For each worker, I have now my own thread pool instance with a max and min number of one thread. Well, it's not the idea of a thread pool, but it makes it very easy to handle the work-items and it also has some nice other featrues. The web application is running on MTA now.
I'm going to prepare some load tests to see if its stable over hours.
see here: http://www.codeproject.com/Articles/7933/Smart-Thread-Pool
I'm developing an addin for a commercial application to expose very simple informations about document opened in that application and so on. The applications itself don't support COM or NET addin but only C++ addin in a DLL.
I want to expose some informations to COM clients and some informations NET clients running in the same machine.
For COM clients I developed in C# an implementation of a COM interface; then I create an object when the host application start and I register the object in the Running Object Table. Deriving this implementation from StandardOleMarshalObject appear sufficient to solve threading issue: apparently the methods of my implementation are called only on main UI thread at message boundary, which is a requirement.
And for NET clients? I need different informations and I don't need to rely on a specific COM interface to expose these information. I was tempted to develop and host a WCF service, but my service methods are called on non-UI therad and the host application crash. How can I replicate the StandardOleMarshalObject behavior in a WCF context? I need to implement an IOperationInvoker? Any suggestion to do this?
Thanks to all and please excuse my bad english.
You should use SynchronizationContext to advance processing to the UI thread. You can explicitly call SynchronizationContext.Send in each service operation implementation or, if there are many operations to implement, you can implement posting to sync context as aspect in IOperationInvoker.