How to call method sendResponse
to cross threaded call?
void watcher_Created(object sender, FileSystemEventArgs e) {
System.Diagnostics.Debug.WriteLine(e.FullPath);
sendResponse(e.FullPath); //this method must causes cross threaded call
}
I was trying :
Deployment.Current.Dispatcher.BeginInvoke(() => {
});
or Application.Current.Dispatcher.Invoke
But there is no Current
.
How to deal with that?
I tried also:
if (this.InvokeRequired) {...}
But there is no InvokeRequired.
EDIT I get unsupported exception when I create any object like Bitmap
in sendResponse
method. This method is called after firing event. So I thought it is crossthreading.
void watcher_Created(object sender, FileSystemEventArgs e) {
System.Diagnostics.Debug.WriteLine(e.FullPath);
sendResponse(e.FullPath);
}
private void sendResponse(string path) {
try {
BitmapImage bmi = new BitmapImage(new Uri(@path, UriKind.Relative));
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmi));
using (MemoryStream ms = new MemoryStream()) {
encoder.Save(ms);
data = ms.ToArray();
}
clientStream.Write(data, 0, data.Length);
} catch (Exception e) { System.Diagnostics.Debug.WriteLine(e); }
}
EDIT2 Client is widnows phone.. this(client) stopped worked after using code from the Nikita's answer(server stopped trhowing exceptions)
try {
Deployment.Current.Dispatcher.BeginInvoke(() => {
MemoryStream stream = new MemoryStream(e.Buffer, e.Offset, e.BytesTransferred);
BitmapImage im = new BitmapImage();
im.SetSource(stream);
MainPage.page.imagePanel.Source = im;
});