我是C#的新手,我主要用VB开发.net应用程序。
我开发了.net WPF应用程序来连接微软认知服务以获取图像描述并保存到sql server。
因为我将它作为计划任务运行,因此我希望它在完成该过程后自动关闭。
应用程序可以正常工作以获取图像描述和更新表。
但如果我添加关闭的应用程序功能,它将立即关闭而不启动该过程。
我可以知道应用程序在关闭应用程序之前如何等待进程完成?
提前致谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.ProjectOxford.Vision;
using Microsoft.ProjectOxford.Vision.Contract;
using System.Diagnostics;
namespace TagPhotoScheduler
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Boolean Finished = false;
public MainWindow()
{
InitializeComponent();
ReadFile();
CloseApp();
}
public void CloseApp()
{
//close application after run
if (Finished == true)
{
Application.Current.Shutdown();
//this.Close();
}
}
public void ReadFile()
{
DataClassesTagPhotoDataContext dc = new DataClassesTagPhotoDataContext();
var query = from qPhoto in dc.db_PhotoLibrary_Photos
where qPhoto.ML_Processed == false
select qPhoto;
foreach (var q in query)
{
string pFileName = string.Empty;
string photoURL = string.Empty;
pFileName = q.PhotoID + "." + q.FileType;
_status.Text += pFileName + " | ";
photoURL = "https://example.com/storage/";
if (q.ProjectID == null || q.ProjectID == 0)
{
GetTag(q.PhotoID, photoURL + pFileName);
_status.Text += photoURL + pFileName + " \n";
}
else //had projectid
{
photoURL += q.ProjectID + "/";
if (q.AlbumID == null || q.AlbumID == 0)
{
//photoURL += "/" + q.AlbumID + "/";
GetTag(q.PhotoID, photoURL + pFileName);
_status.Text += photoURL + pFileName + " \n";
}
else //albumid
{
photoURL += q.AlbumID + "/";
GetTag(q.PhotoID, photoURL + pFileName);
_status.Text += photoURL + pFileName + " \n";
}
}
}
//Finished = true;
_status.Text += "\n\nFinished";
}
public async void GetTag(int PhotoID, string URL)
{
VisionServiceClient VisionServiceClient = new VisionServiceClient("xxxxxxxxxxxxxxxxxxxx", "https://eastasia.api.cognitive.microsoft.com/vision/v1.0");
DataClassesTagPhotoDataContext dc = new DataClassesTagPhotoDataContext();
try
{
AnalysisResult analysisResult = await VisionServiceClient.DescribeAsync(URL, 3);
if (analysisResult.Description != null)
{
foreach (var tag in analysisResult.Description.Tags)
{
tags += tag + ", ";
string QueryAnalysis = string.Empty;
db_PhotoLibrary_Photo_Tag newQ = new db_PhotoLibrary_Photo_Tag();
newQ.TagOn = DateTime.Now;
newQ.TagBy = 0;
newQ.PhotoID = PhotoID;
newQ.ML_Tag = true;
newQ.TagType = 0;
newQ.TagName = tag;
dc.db_PhotoLibrary_Photo_Tags.InsertOnSubmit(newQ);
dc.SubmitChanges();
db_PhotoLibrary_Photo result = (from p in dc.db_PhotoLibrary_Photos
where p.PhotoID == PhotoID
select p).SingleOrDefault();
result.ML_Processed = true;
dc.SubmitChanges();
}
_status.Text = tags;
}
}
catch { }
}
}
}
最后,我将其更改为控制台应用程序,它可以完成我的工作。
答案 0 :(得分:0)
您可以使用任务包装ReadFile函数并使用continuation:
public MainWindow()
{
InitializeComponent();
Task.Run(ReadFile).ContinueWith(CloseApp);
}
你甚至确定这需要是WPF应用吗?控制台无法完成这项工作?