WPF:应用程序正在被绞死。不知道为什么? [英] WPF: Application is getting hanged. not sure why?
问题描述
大家好,
我有一个程序打开excel并检查给定表单中的链接是否断开。我想向用户显示给定链接列表以及哪些链接已损坏。我想将前景改为RED。我已将值绑定到observablecollection,集合只是字符串集合(WEB LINKS)并且工作正常。逻辑运行良好。
与列表视图绑定的部分也正常工作。
现在我想改变颜色URL的'破坏了RED。到现在为止,我没有成功。我在搜索时尝试了很少的代码。并更改了XAML。
现在应用程序在代码运行时挂起。特别是当它检查网站时。
原谅我正在学习的糟糕编码
XAML: -
< Window x:Class =URLValide.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns: x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:d =http://schemas.microsoft.com/expression/blend/2008
xmlns: Local =clr-namespace:URL_Validater.URLValide
Title =URL ValidatorHeight =269Width =525FontSize =13
ResizeMode =NoResize>
< Window.Resources>
< ResourceDictionary>
< Style x:Key =HighLightTargetType ={x:Type Control}>
< Setter Property =BackgroundValue =Honeydew/>
< Style.Triggers>
< Trigger Property =IsFocusedValue =True>
< Setter Property =Effect>
< Setter.Value>
< DropShadowEffect BlurRadius =40Color =BeigeDirection =50Opacity =0.5/>
< /Setter.Value>
< / Setter>
< Setter Property =BackgroundValue =White/>
< Setter Property =FontWeightValue =UltraBlack/>
< / Trigger>
< /Style.Triggers>
< / Style>
<! - < Style TargetType ={x:Type ListViewItem}>
< Style.Triggers>
< DataTrigger Binding ={Binding ColorIndx}Value =True>
< Setter Property =BackgroundValue =Red/>
< Setter Property =ForegroundValue =White/>
< / DataTrigger>
< /Style.Triggers>
< / Style> - >
< / ResourceDictionary>
< /Window.Resources>
< DockPanel LastChildFill =True>
< Label DockPanel.Dock =TopName =WinHeaderHeight =30Background =BurlyWoodFontFamily =CalibriFontSize =20FontWeight =BoldFontStretch =MediumVerticalAlignment =TopHorizontalAlignment =StretchMargin =0,0,0,0HorizontalContentAlignment =CenterVerticalContentAlignment =Top> URL Validator< / Label>
< Grid DockPanel.Dock =Bottomremoved =Beige>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =auto/>
< ColumnDefinition Width =auto/>
< ColumnDefinition Width =auto/>
< /Grid.ColumnDefinitions>
< Grid.RowDefinitions>
< RowDefinition Height =20MaxHeight =30/>
< /Grid.RowDefinitions>
< TextBlock Grid.Column =0Text =当前网址:>< / TextBlock>
< TextBlock Grid.Column =1Text ={Binding strCrnUrl,UpdateSourceTrigger = PropertyChanged}Width =370>< / TextBlock>
< TextBlock Grid.Column =2Text ={Binding strTotlOf,UpdateSourceTrigger = PropertyChanged}HorizontalAlignment =LeftVerticalAlignment =Center>< / TextBlock>
< / Grid>
< Grid DockPanel.Dock =RightWidth =154>
< Grid.ColumnDefinitions>
< ColumnDefinition MaxWidth =300Width =19 */>
< /Grid.ColumnDefinitions>
< Grid.RowDefinitions>
< RowDefinition MaxHeight =55MinHeight =10Height =35 */>
< RowDefinition Height =165 *MinHeight =10/>
< /Grid.RowDefinitions>
< Label Name =LbleRightBorderBrush =BlackBorderThickness =1Margin =0,2FontSize =13Content =URL列表'的高度= 30Grid.ColumnSpan =2Grid.Row =0>< / Label>
< ListView Grid.Row =1removed =AzureItemsSource ={Binding strPdfLst}IsEnabled =TrueMargin =4,0,0,1Foreground ={Binding ColorIndx} >
< / ListView>
< / Grid>
< Grid DockPanel.Dock =Left>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =100 */>
< /Grid.ColumnDefinitions>
< Grid.RowDefinitions>
< RowDefinition Height =30 */>
< RowDefinition Height =60 */>
< /Grid.RowDefinitions>
< Label BorderBrush =BlackName =lblFleSelBorderThickness =1Height =27Margin =2,2,0,30HorizontalAlignment =LeftWidth =349>选择包含URL列表的文件:< / Label>
< TextBox Name =txtbxFleNmeBorderBrush =BlackBorderThickness =1Margin =2,34,0,0Style ={StaticResource ResourceKey = HighLight}HorizontalAlignment =LeftWidth = 272 >< /文本框>
< Button Name =btnFleSlecWidth =69Height =27Content =BrowseHorizontalAlignment =CenterVerticalAlignment =CenterMargin =276,32,4,0> < /按钮>
< Grid Grid.Row =1Height =118HorizontalAlignment =LeftMargin =0,1,0,0Name =OptionGridVerticalAlignment =TopGrid.RowSpan = 2\" >
< StackPanel HorizontalAlignment =LeftVerticalAlignment =TopOrientation =Horizontal>
< Border BorderBrush =BlackBorderThickness =1Height =117Margin =3,1,0,0HorizontalAlignment =StretchWidth =350>
< Grid Height =autoHorizontalAlignment =StretchWidth =335>
< Grid.RowDefinitions>
< RowDefinition Height =10 */>
< RowDefinition Height =10 */>
< RowDefinition Height =10 */>
< RowDefinition Height =10 */>
< RowDefinition Height =10 */>
< /Grid.RowDefinitions>
< CheckBox Name =chkboxHasVerticalAlignment =CenterHorizontalAlignment =Center>具有列标题< / CheckBox>
< Label Name =lblDescHeight =autoMargin =0,0,204,0Grid.Row =1HorizontalContentAlignment =RightVerticalAlignment =Top>描述列:< /标签>
< ComboBox Grid.Row =1Name =cmboxDescolMargin =131,0,0,0Style ={StaticResource ResourceKey = HighLight}>< / ComboBox>
< Label Name =lblUrlColHeight =autoGrid.Row =2Margin =0,0,204,0HorizontalContentAlignment =Right> URL'的列:< / Label> ;
< ComboBox Name =cmboxUrlColGrid.Row =2Margin =131,0,0,0Style ={StaticResource ResourceKey = HighLight}>< / ComboBox>
< Label Name =lblResColHeight =autoGrid.Row =3Margin =0,0,196,0HorizontalContentAlignment =Right> Result Column:< / Label>
< ComboBox Name =cmboxResColGrid.Row =3Margin =131,0,0,0Style ={StaticResource ResourceKey = HighLight}>< / ComboBox>
< Button Grid.Row =4HorizontalAlignment =CenterVerticalAlignment =CenterContent =Validate URL linksName =btnValidate>< / Button>
< Button Grid.Row =4HorizontalAlignment =CenterVerticalAlignment =CenterContent =StartName =btnstrtMargin =244,0,56,0>< /按钮>
< / Grid>
< / Border>
< / StackPanel>
< / Grid>
< / Grid>
< / DockPanel>
< / Window>
VB.net类: -
Imports Microsoft
Imports Microsoft
Imports 系统
Imports System.Net
Imports System.Data
Imports System.Windows
Imports System.Windows.Forms
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Collections.ObjectModel
Imports System.ComponentModel
命名空间 URLValide
公共 类 clsUrlCheck
实施 INotifyP ropertyChanged
公共 事件 propertyChanged 正如 PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
公共 exclApplic 作为 新 Excel.Application
公开 exclWkbOpe As Excel.Workbook
公共 exclWksAct As Excel.Worksheet
Public exclRngUrl As Excel.Range
< span class =code-keyword> Public exclRngDes As Excel.Range
Public exclRngOut A. s Excel.Range
公共 intMaxRow 作为 长
公共 intCrtRow 作为 长
私有 _strTotlOf 作为 String
Private _strCrnUrl As String
Private _ColorIndx As SolidColorBrush
Private _strPdfLst As ObservableCollection( 字符串)
私有 _strhdrPdf As ObservableCollection( Of String )
< span class =code-preprocessor>#Regionpoperties的区域
公共 属性 strTotlOf()作为 字符串
获取
返回 _strTotlOf
结束 获取
设置( ByVal 值 As String )
_strTotlOf = value
Me .OnPropertyChanged( strTotlOf)
结束 设置
结束 属性
公共 属性 strCrnUrl()作为 字符串
获取
返回 _strCrnUrl
结束 获取
设置( ByVal 值作为 字符串)
_strCrnUrl = value
我。 OnPropertyChanged( strCrnUrl)
End 设置
结束 属性
公共 属性 strPdfLst As ObservableCollection( Of String )
获取
返回 _strPdfLst
结束 获取
设置( ByVal 值< span class =code-keyword> As ObservableCollection( Of String ))
_strPdfLst = value
Me .OnPropertyChanged( strPdfLst)
结束 设置
结束 财产
Public Property strhdrPdf As ObservableCollection( Of 字符串)
获取
返回 _strhdrPdf
结束 获取
< span class =code-keyword> Set ( ByVal value As ObservableCollection( String ))
_strhdrPdf = value
Me .OnPropertyChanged( strhdrPdf)
结束 设置
结束 属性
公共 属性 ColorIndx 作为 SolidColorBrush
获取
返回 _ColorIndx
结束 获取
设置( ByVal 值 As SolidColorBrush)
_ColorIndx = value
我 .OnPropertyChanged( ColorIndx)
结束 设置
结束 物业
私有 Sub OnPropertyChanged( ByVal propertyName < span class =code-keyword>作为 字符串)
RaiseEvent propertyChanged ( Me ,新 PropertyChangedEventArgs(propertyName))
结束 Sub
#End Region
#Region验证URL的功能
公共 功能 UrlCheck( ByVal strUrlReq As 字符串)
尝试
Dim WebReq As Net.HttpWebRequest = Net.HttpWebRequest.Create(strUrlReq)
WebReq.Method = Head
WebReq.Timeout = 5000
使用 Response = WebReq.GetResponse()
返回 True
结束 使用
Catch ex 作为例外
ColorIndx = 新 SolidColorBrush(Colors.Red)
返回 错误
结束 尝试
结束 功能
#End Region
#RegionStart Funtion
Sub suStart()
如果 exclRngDes.Value = Make Desc 然后
strPdfLst = makeList(exclRngUrl.Column, True )
exclRngDes.Delete()
其他
strPdfL st = makeList(exclRngUrl.Column, False )
结束 如果
Dim blnValid As 布尔值
对于 每个 exclRngEch 作为 Excel.Range 在 exclRngUrl
strCrnUrl = exclRngEch.Text
strTotlOf = exclRngEch.Row& ; OF& intMaxRow
blnValid = UrlCheck(exclRngEch.Value.ToString)
如果 blnValid 那么
exclWksAct.Cells(exclRngEch.Row,exclRngOut.Column).value = Web Page Present
其他
exclWksAct.Cells(exclRngEch.Row,exclRngOut.Column).value = 网页错误
结束 如果
下一步
结束 Sub
功能 makeList( ByVal intColNum 作为 < span class =code-keyword>长, ByVal blnMkeStr As 布尔)作为 ObservableCollection( String )
Dim ObsColTem As ObservableCollection ( 字符串)= 新 ObservableCollection( 字符串)
如果 不 blnMkeStr 然后
For intLopCnt As Int32 = intCrtRow To i ntMaxRow
ObsColTem.Add(exclWksAct.Cells(intLopCnt,intColNum).Text)
Next
否则
对于 intLopCnt 作为 Int32 = intCrtRow intMaxRow
Dim strDesc As String = Mid(exclWksAct.Cells(intLopCnt,intColNum).Text,InStrRev(exclWksAct.Cells(intLopCnt,intColNum) .Text, / ,, CompareMethod.Text)+ 1 )
ObsColTem.Add(strDesc)
下一步
结束 如果
返回 ObsColTem
结束 功能
#End Region
结束 类
结束 命名空间
很抱歉,如果我不清楚的话。
代码运行正常,我得到了输出。
代码没有冻结或停止执行。但WPF表单窗口正在BLANK。
就像你一步一步调试一样,你会看到一个空白的白色窗口仪式。就像它即将到来。当执行完成时,窗口变为活动状态并正确显示相关输出。事件,Excel更新。
所以代码很好。
你的假设是正确的HttpWebRequest .GetResponse
可以冻结用户界面并造成一些麻烦。看看你的代码:你正在使用5秒超时;这意味着您的UI线程可能会挂起5秒钟。我不是说这是唯一的问题,但这是你在UI线程中永远不应该做的事情之一。您必须有一个单独的线程并在那里进行检查,就像阻止任何其他阻塞,可能阻塞调用或持久操作一样。使用单独的线程,您可能还需要使用Dispatcher
进行线程同步和/或UI线程调用。
< DD> -SA
Hi all,
i have a program which opens excel and check for broken links in the given sheet. i want to display the user the list of given links and which ever is broken. i want to change foreground to RED. i have bound the values to observablecollection, the collection is just collection of strings(WEB LINKS) and is working fine. the logic is working good.
the part of binding with the list view is also working.
now i am trying to change the color of the URL''s which are broke to RED. till now i am unsuccessful. and i tried few codes got when searching. and changed the XAML.
now the application is hanging when the code runs. particularly when it is checking for the websites.
Forgive my bad codding i am learning
XAML:-
<Window x:Class="URLValide.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:Local="clr-namespace:URL_Validater.URLValide" Title="URL Validator" Height="269" Width="525" FontSize="13" ResizeMode="NoResize"> <Window.Resources> <ResourceDictionary> <Style x:Key="HighLight" TargetType="{x:Type Control}"> <Setter Property="Background" Value="Honeydew"/> <Style.Triggers> <Trigger Property="IsFocused" Value="True"> <Setter Property="Effect"> <Setter.Value> <DropShadowEffect BlurRadius="40" Color="Beige" Direction="50" Opacity="0.5"/> </Setter.Value> </Setter> <Setter Property="Background" Value="White"/> <Setter Property="FontWeight" Value="UltraBlack"/> </Trigger> </Style.Triggers> </Style> <!--<Style TargetType="{x:Type ListViewItem}"> <Style.Triggers> <DataTrigger Binding="{Binding ColorIndx}" Value="True"> <Setter Property="Background" Value="Red"/> <Setter Property="Foreground" Value="White"/> </DataTrigger> </Style.Triggers> </Style>--> </ResourceDictionary> </Window.Resources> <DockPanel LastChildFill="True"> <Label DockPanel.Dock="Top" Name="WinHeader" Height="30" Background="BurlyWood" FontFamily="Calibri" FontSize="20" FontWeight="Bold" FontStretch="Medium" VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="0,0,0,0" HorizontalContentAlignment="Center" VerticalContentAlignment="Top">URL Validator</Label> <Grid DockPanel.Dock="Bottom" removed="Beige"> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="20" MaxHeight="30"/> </Grid.RowDefinitions> <TextBlock Grid.Column="0" Text="Current URL:"></TextBlock> <TextBlock Grid.Column="1" Text="{Binding strCrnUrl, UpdateSourceTrigger=PropertyChanged}" Width="370"></TextBlock> <TextBlock Grid.Column="2" Text="{Binding strTotlOf, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center"></TextBlock> </Grid> <Grid DockPanel.Dock="Right" Width="154"> <Grid.ColumnDefinitions> <ColumnDefinition MaxWidth="300" Width="19*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition MaxHeight="55" MinHeight="10" Height="35*" /> <RowDefinition Height="165*" MinHeight="10" /> </Grid.RowDefinitions> <Label Name="LbleRight" BorderBrush="Black" BorderThickness="1" Margin="0,2" FontSize="13" Content="List Of URL''s" Height="30" Grid.ColumnSpan="2" Grid.Row="0"></Label> <ListView Grid.Row="1" removed="Azure" ItemsSource="{Binding strPdfLst}" IsEnabled="True" Margin="4,0,0,1" Foreground="{Binding ColorIndx}"> </ListView> </Grid> <Grid DockPanel.Dock="Left"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="30*" /> <RowDefinition Height="60*" /> </Grid.RowDefinitions> <Label BorderBrush="Black" Name="lblFleSel" BorderThickness="1" Height="27" Margin="2,2,0,30" HorizontalAlignment="Left" Width="349">Select File To which Contains the list of URL''s:</Label> <TextBox Name="txtbxFleNme" BorderBrush="Black" BorderThickness="1" Margin="2,34,0,0" Style="{StaticResource ResourceKey=HighLight}" HorizontalAlignment="Left" Width="272"></TextBox> <Button Name="btnFleSlec" Width="69" Height="27" Content="Browse" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="276,32,4,0"></Button> <Grid Grid.Row="1" Height="118" HorizontalAlignment="Left" Margin="0,1,0,0" Name="OptionGrid" VerticalAlignment="Top" Grid.RowSpan="2"> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal"> <Border BorderBrush="Black" BorderThickness="1" Height="117" Margin="3,1,0,0" HorizontalAlignment="Stretch" Width="350"> <Grid Height="auto" HorizontalAlignment="Stretch" Width="335"> <Grid.RowDefinitions> <RowDefinition Height="10*"/> <RowDefinition Height="10*"/> <RowDefinition Height="10*"/> <RowDefinition Height="10*"/> <RowDefinition Height="10*"/> </Grid.RowDefinitions> <CheckBox Name="chkboxHas" VerticalAlignment="Center" HorizontalAlignment="Center">Has Column Headers</CheckBox> <Label Name="lblDesc" Height="auto" Margin="0,0,204,0" Grid.Row="1" HorizontalContentAlignment="Right" VerticalAlignment="Top">Description Column:</Label> <ComboBox Grid.Row="1" Name="cmboxDescol" Margin="131,0,0,0" Style="{StaticResource ResourceKey=HighLight}"></ComboBox> <Label Name="lblUrlCol" Height="auto" Grid.Row="2" Margin="0,0,204,0" HorizontalContentAlignment="Right">URL''s Column:</Label> <ComboBox Name="cmboxUrlCol" Grid.Row="2" Margin="131,0,0,0" Style="{StaticResource ResourceKey=HighLight}"></ComboBox> <Label Name="lblResCol" Height="auto" Grid.Row="3" Margin="0,0,196,0" HorizontalContentAlignment="Right">Result Column:</Label> <ComboBox Name="cmboxResCol" Grid.Row="3" Margin="131,0,0,0" Style="{StaticResource ResourceKey=HighLight}"></ComboBox> <Button Grid.Row="4" HorizontalAlignment="Center" VerticalAlignment="Center" Content="Validate URL links" Name="btnValidate"></Button> <Button Grid.Row="4" HorizontalAlignment="Center" VerticalAlignment="Center" Content="Start" Name="btnstrt" Margin="244,0,56,0"></Button> </Grid> </Border> </StackPanel> </Grid> </Grid> </DockPanel> </Window>
VB.net Class:-
Imports Microsoft
Imports Microsoft
Imports System
Imports System.Net
Imports System.Data
Imports System.Windows
Imports System.Windows.Forms
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Namespace URLValide
Public Class clsUrlCheck
Implements INotifyPropertyChanged
Public Event propertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public exclApplic As New Excel.Application
Public exclWkbOpe As Excel.Workbook
Public exclWksAct As Excel.Worksheet
Public exclRngUrl As Excel.Range
Public exclRngDes As Excel.Range
Public exclRngOut As Excel.Range
Public intMaxRow As Long
Public intCrtRow As Long
Private _strTotlOf As String
Private _strCrnUrl As String
Private _ColorIndx As SolidColorBrush
Private _strPdfLst As ObservableCollection(Of String)
Private _strhdrPdf As ObservableCollection(Of String)
#Region "Region of poperties"
Public Property strTotlOf() As String
Get
Return _strTotlOf
End Get
Set(ByVal value As String)
_strTotlOf = value
Me.OnPropertyChanged("strTotlOf")
End Set
End Property
Public Property strCrnUrl() As String
Get
Return _strCrnUrl
End Get
Set(ByVal value As String)
_strCrnUrl = value
Me.OnPropertyChanged("strCrnUrl")
End Set
End Property
Public Property strPdfLst As ObservableCollection(Of String)
Get
Return _strPdfLst
End Get
Set(ByVal value As ObservableCollection(Of String))
_strPdfLst = value
Me.OnPropertyChanged("strPdfLst")
End Set
End Property
Public Property strhdrPdf As ObservableCollection(Of String)
Get
Return _strhdrPdf
End Get
Set(ByVal value As ObservableCollection(Of String))
_strhdrPdf = value
Me.OnPropertyChanged("strhdrPdf")
End Set
End Property
Public Property ColorIndx As SolidColorBrush
Get
Return _ColorIndx
End Get
Set(ByVal value As SolidColorBrush)
_ColorIndx = value
Me.OnPropertyChanged("ColorIndx")
End Set
End Property
Private Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent propertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
#End Region
#Region "function of Validating the URL's"
Public Function UrlCheck(ByVal strUrlReq As String)
Try
Dim WebReq As Net.HttpWebRequest = Net.HttpWebRequest.Create(strUrlReq)
WebReq.Method = "Head"
WebReq.Timeout = 5000
Using Response = WebReq.GetResponse()
Return True
End Using
Catch ex As Exception
ColorIndx = New SolidColorBrush(Colors.Red)
Return False
End Try
End Function
#End Region
#Region "Start Funtion"
Sub suStart()
If exclRngDes.Value = "Make Desc" Then
strPdfLst = makeList(exclRngUrl.Column, True)
exclRngDes.Delete()
Else
strPdfLst = makeList(exclRngUrl.Column, False)
End If
Dim blnValid As Boolean
For Each exclRngEch As Excel.Range In exclRngUrl
strCrnUrl = exclRngEch.Text
strTotlOf = exclRngEch.Row & "OF" & intMaxRow
blnValid = UrlCheck(exclRngEch.Value.ToString)
If blnValid Then
exclWksAct.Cells(exclRngEch.Row, exclRngOut.Column).value = "Web Page Present"
Else
exclWksAct.Cells(exclRngEch.Row, exclRngOut.Column).value = "Web Page Error"
End If
Next
End Sub
Function makeList(ByVal intColNum As Long, ByVal blnMkeStr As Boolean) As ObservableCollection(Of String)
Dim ObsColTem As ObservableCollection(Of String) = New ObservableCollection(Of String)
If Not blnMkeStr Then
For intLopCnt As Int32 = intCrtRow To intMaxRow
ObsColTem.Add(exclWksAct.Cells(intLopCnt, intColNum).Text)
Next
Else
For intLopCnt As Int32 = intCrtRow To intMaxRow
Dim strDesc As String = Mid(exclWksAct.Cells(intLopCnt, intColNum).Text, InStrRev(exclWksAct.Cells(intLopCnt, intColNum).Text, "/", , CompareMethod.Text) + 1)
ObsColTem.Add(strDesc)
Next
End If
Return ObsColTem
End Function
#End Region
End Class
End Namespace
Sorry if i was not clear.
The code is running correctly and i am getting the out put.
The code is not freezing or stopping the execution. but the WPF form window is going BLANK.
like when you go for step by step debug, you will see just a blank white window rite. like that it is coming. when the execution is complete the window comes alive and shows the related output correctly. event the Excel is updated.
So code is good.
You are right in your assumption thatHttpWebRequest.GetResponse
can freeze the UI and cause some trouble. Look at your code: you are using 5 sec timeout; it means that your UI thread may hang for 5 sec. I''m not saying that this is the only problem, but this is one of the thing you should never do in your UI thread. You have to have a separate thread and do your check there, as with any other blocking, potentially blocking calls or long-lasting operations. Using a separate thread, you may also need thread synchronization and/or UI thread invocation usingDispatcher
.
—SA
这篇关于WPF:应用程序正在被绞死。不知道为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!