I am developing a GUI application using WPF in Visual Studio. I am developing an asynchronous process.
The contents of the process
- Asynchronous processing starts when button is pressed
- Check the path corresponding to the list regularly (once per second) and copy it to the work file storage (loop start)
- Specify the corresponding file copied to Image.Souce (depiction)
- Processing time is drawn in ViewList
- Return to loop start
It is a feeling. Only the relevant part is described. This code compiles fine, but I can't see the image in the Image window of my GUI app. On the other hand, similarly, the additional processing to listView described in Invoke as the UI processing part can be depicted well. I'm sorry I've been stuck for a long time, but I would like to ask for your advice. In addition, I thought that File.delete might be bad, so I erased it, but it was useless.
private async void show_image (object sender, RoutedEventArgs e)
{
ShowButton.IsEnabled = false;
try
{
await Task.Run (() =>
{
while (true)
{
// ---------- async loop start ------------ /
String origin_image_path = "**. Jpg";
String dst_image_path = "***. Jpg"
File.Copy (origin_image_path, dst_image_path, true);
// lock less bitmap
MemoryStream data = new MemoryStream (File.ReadAllBytes (dst_image_path));
WriteableBitmap wbmp = new WriteableBitmap (BitmapFrame.Create (data));
data.Close ();
this.Dispatcher.Invoke ((Action) (() =>
{
listView.Items.Add (new string [] {str_date, "Start"});
listView.Items.Add (new string [] {end_date, "End"});
this.MainImage.Source = wbmp;
}));
File.Delete (dst_image_path);
System.Threading.Thread.Sleep (1000/60);
// ------------- async loop end --------------- /
}
});
}
catch
{
}
finally
{
ShowButton.IsEnabled = true;
}
}
-
Answer # 1
-
Answer # 2
Let's crush each step by step.
Does the property of WriteableBitmap have the expected value?
Is the image displayed if you use synchronous processing instead of asynchronous processing?
Is it displayed as a single image with no loops?
If you put MemoryStream ~ File.Delete in Invoke somehow, it seems to work normally.
As far as I read the annotation of MS's WritableBitmap class, WriteableBitmap is tied to UI thread, so if you want to access it asynchronously, it is correct to create WriteableBitmap on the UI thread side beforehand and lock it on the Task side to write. Seems like a procedure.
WriteableBitmap classThere seems to be a hand not to use WriteableBitmap itself.
WritePixels of WriteableBitmap in a Task-Stack Overflow
Related articles
- i want to print the specified book of xlsx file in c #
- c # - null cannot be specified when inserting into the autoincrement column
- if you connect the specified number of images horizontally in c #, you want to start a new line and connect them horizontally in
- c # - i want to interrupt asynchronous communication
- c # - how do i change the selectedindex of a combobox in a specified row of a combobox in a wpf datagrid?
- c # - [unity] even though "has exit time" of animator is specified, it will transition to another state earlier than t
- c # - ffmpeg, how to add audio files from a specified time
- c # - cannot be specified as a relative path to openfiledialoginitialdirectory
- c # - i want to display an object when the specified number of taps is reached
- c # - wpf i want to display the datagrid with the specified column of the acquired csv
- c # - i want to get a specified number at random
- c # i want to get all file paths that do not contain the specified string
- c # linq i want to enumerate a specified number
- c # - invoke does not work well
- c # - move only at specified angle
- c # - get variable values from aspnet code-behind
- unable to add c # systemwindowsformsform
- c # - i want to display my form as new after sleeping for a few seconds
- c # - i want to share variables from other scripts
- get audio in c # and display waveform
- c # - i want to specify a different url from the script side for the video player component of unity
- c # - image processing flow (detection of items on paper)
- candidates are not displayed even if i hit a period in c # opened in unity
- c # - a transformation matrix is created from the feature points of the two images
- c # - the image after afine conversion becomes smaller
The following part
I was able to manage it by doing the following.
The handling of WPF Bitmap is complicated.