Переведено с помощью DeepL.com (бесплатная версия)
-коды раздела, где находится кнопка и выполняется операция -
Код: Выделить всё
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
using System.IO;
using System.Drawing.Printing;
using PdfiumViewer;
namespace Printer
{
public partial class UserControl3Faturalar : UserControl
{
private Timer _timer;
public UserControl3Faturalar()
{
InitializeComponent();
}
private async void UserControl3Faturalar_Load(object sender, EventArgs e)
{
await LoadDataAsync();
// Timer to refresh data every 2 minutes
_timer = new Timer();
_timer.Interval = 120000; // 2 minutes (120000 ms)
_timer.Tick += async (s, args) => await LoadDataAsync();
_timer.Start();
}
private async Task LoadDataAsync()
{
try
{
string baseUrl = ConfigReader.GetValue("url").TrimEnd('/');
string userId = ConfigReader.GetValue("userId").Trim('/');
string apiKey = ConfigReader.GetValue("x-api-key");
string requestUrl = $"{baseUrl}/api/users/{userId}/pdfs";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("x-api-key", apiKey);
HttpResponseMessage response = await client.GetAsync(requestUrl);
response.EnsureSuccessStatusCode();
string responseData = await response.Content.ReadAsStringAsync();
var dataList = JsonConvert.DeserializeObject(responseData);
dataGridView1.DataSource = dataList;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
}
catch (HttpRequestException httpEx)
{
MessageBox.Show($"HTTP Error: {httpEx.Message}");
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}");
}
}
private async void button1_Click(object sender, EventArgs e)
{
string printerName = GlobalConfig.SelectedPrinter;
if (string.IsNullOrEmpty(printerName))
{
MessageBox.Show("Please select a printer in the Yazicilar section.");
return;
}
try
{
string baseUrl = ConfigReader.GetValue("url").TrimEnd('/');
string userId = ConfigReader.GetValue("userId").Trim('/');
string apiKey = ConfigReader.GetValue("x-api-key");
string requestUrl = $"{baseUrl}/api/users/{userId}/pdfs";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("x-api-key", apiKey);
HttpResponseMessage response = await client.GetAsync(requestUrl);
response.EnsureSuccessStatusCode();
string responseData = await response.Content.ReadAsStringAsync();
var pdfDataList = JsonConvert.DeserializeObject
Подробнее здесь: [url]https://stackoverflow.com/questions/78828732/c-sharp-pdfium-printer-closing-dialog[/url]