我在form1中添加了一个新窗体,我有一个按钮单击事件:
private void button1_Click(object sender, EventArgs e)
{
UploadTestingForum utf = new UploadTestingForum();
utf.Show();
}
新的表单代码我从这里的例子:
https://github.com/youtube/api-samples/blob/master/dotnet/UploadVideo.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Threading;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
namespace Youtube_Manager
{
public partial class UploadTestingForum : Form
{
string errors = "";
public UploadTestingForum()
{
InitializeComponent();
try
{
new UploadTestingForum().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
//Console.WriteLine("Error: " + e.Message);
errors = e.Message;
}
}
}
private async Task Run()
{
UserCredential credential;
using (var stream = new FileStream(@"D:\C-Sharp\Youtube-Manager\Youtube-Manager\Youtube-Manager\bin\Debug\client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
var video = new Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = "Default Video Title";
video.Snippet.Description = "Default Video Description";
video.Snippet.Tags = new string[] { "tag1", "tag2" };
video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
var filePath = @"D:\tester\20131207_134823.mp4"; // Replace with path to actual movie file.
using (var fileStream = new FileStream(filePath, FileMode.Open))
{
var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
await videosInsertRequest.UploadAsync();
}
}
void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
switch (progress.Status)
{
case UploadStatus.Uploading:
//Console.WriteLine("{0} bytes sent.", progress.BytesSent);
label1.Text = progress.BytesSent.ToString();
break;
case UploadStatus.Failed:
//Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
label1.Text = progress.Exception.ToString();
break;
}
}
void videosInsertRequest_ResponseReceived(Video video)
{
//Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
label2.Text = video.Id;
}
private void UploadTestingForum_Load(object sender, EventArgs e)
{
}
}
}
但是,一旦我单击了form1按钮,我就使用了一个断点,我看到它正在执行一个循环,它一直在执行以下行:string errors = "";
和new UploadTestingForum().Run().Wait();
几秒钟后,我在窗体设计器的cs文件中的以下行中遇到异常:this.ResumeLayout(false);
An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll
System.StackOverflowException was unhandled
我想做的是,当我在form1中单击button1时,它将首先显示新窗体,然后执行新窗体中的代码。
2条答案
按热度按时间ht4b089n1#
看这行字:
请记住,这是表单的建构函式的一部分。当程式码执行时,它会建立表单的另一个执行严修,并执行它的建构函式。此时,您会再次执行相同的程式码,建立另一个执行严修,并执行它的建构函式,然后再次执行这个程式码,建立表单的另一个执行严修...
每次构造函数运行另一个方法调用时,调用堆栈都会继续。由于您创建了一个新示例,并在方法返回之前再次调用构造函数,因此堆栈条目永远不会被清除/弹出。很快,调用堆栈就会耗尽空间并 * 溢出 *,因此会出现StackOverflowException。
将该行更改为:
lyr7nygr2#
更改行
至
你从构造函数调用构造函数。我认为你可能仍然有问题,因为你从构造函数调用方法,在窗体显示之前,但一次一件事。