D
D
DarkByte20152016-04-16 08:37:14
WPF
DarkByte2015, 2016-04-16 08:37:14

Why is the error falling?

Here is the client code:

private void btChangeState_Click(object sender, RoutedEventArgs e)
{
    if (!IsConnected)
    {
        Exception error = null;

        if (!TryConnect($"net.tcp://{tbIP.Text}:{tbPort.Text}/JobService", out Proxy, out error))
        {
            MessageBox.Show(error.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            return;
        }
    }
    else
    {
        Proxy.Close();
    }

    IsConnected = !IsConnected;
    Jobs.Clear();
}

private bool TryConnect(string endpoint, out JobServiceClient proxy, out Exception error)
{
    try
    {
        var callback = new ClientCallback();
        callback.GiveJob += Callback_GiveJobAsync;
        var ctx = new InstanceContext(callback);
        proxy = new JobServiceClient(ctx);
        proxy.Endpoint.Address = new EndpointAddress(endpoint);
        proxy.Connect();
        error = null;
        return true;
    }
    catch (Exception e)
    {
        proxy = null;
        error = e;
        return false;
    }
}

Why do I get this error when I call Connect? "This CollectionView type does not support changes to its SourceCollection from a thread other than the Dispatcher thread." No additional threads are used in this case, everything is basic.
Now it is not the client on which the error pops up that is debugging, but the server, where the Connect function is actually called through a proxy. Looks like the error is right there. But I still don’t understand why it pops up... There is a collection in the service contract:
public ObservableCollection<ClientView> Clients { get; } = new ObservableCollection<ClientView>();
In the Connect function, a ClientView object is just created and added to it. And it is with this addition (according to the debugger) that an error is thrown, which is simply transmitted to the client. But everything happens in one thread, there are no extra threads there! Where does the error come from?
PS Dispatcher.Invoke doesn't help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DarkByte2015, 2016-04-16
@DarkByte2015

As it turned out empirically, the problem was that the button that launched the server had an asynchronous handler. It is not clear exactly how, but somehow he influenced this, since it was worth making it synchronous, as the error disappeared.

L
Larry Underwood, 2016-04-18
@Hydro

No additional threads are used in this case, everything is basic.

This is what you think.
WCF hides a lot of logic underneath that can run on threads other than the main one.
The error that you caught explicitly indicates that the addition to the collection does not occur on the UI thread.
To fix it, it was necessary to call Invoke from the Dispatcher of the application, and only in this way, through Application.Current.Dispatcher.Invoke. And Dispatcher.Current can be completely different, depending on the synchronization context.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question