Answer the question
In order to leave comments, you need to log in
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;
}
}
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? Answer the question
In order to leave comments, you need to log in
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.
No additional threads are used in this case, everything is basic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question