检测线程何时关闭(Detecting when thread has been closed)

在我的C#应用程序中,我有一个单独的窗口,其中包含一个在单独的线程中运行的进度条。 每隔30秒,它会从服务器发送数据请求并相应地填充进度条。

问题是,当我关闭主程序时,带有进度条的第二个窗口保持打开状态。

如何编写第二个线程以检测第一个线程何时关闭,并相应地关闭它自己?


In my C# application, I have a separate window containing a progress bar that runs in a separate thread. Every 30 seconds it sends a request for data from a server and fills the progress bar accordingly.

The problem is, when I close the main program, the second window with the progress bar stays open.

How can I program the second thread to detect when the first thread has been closed, and close itself accordingly?


原文:https://stackoverflow.com/questions/15838077
2023-06-02 10:06

满意答案

是的,您拥有的代码将复制指针,而不是复制指针。 你需要复制每个子model3的内容,以及它们的子model3model3model2model3 ,你没有提供定义。所以类似的东西(如果model1等只是double [])...

parameters current;

double checkem[MAX_PARS];
double *p = &checkem[0];

// copy X into checkem
memcpy(p, &current.x, numx * sizeof(struct model1));
p += (numx * sizeof(struct model1)) / sizeof(double);

// copy Y into checkem after X
memcpy(p, &current.y, numy * sizeof(struct model2));
p += (numy * sizeof(struct model2)) / sizeof(double);

// copy Z into checkem after Y
memcpy(p, &current.z, numz * sizeof(struct model3));

如果model3model2model3比双精度数组更复杂,则需要分别复制双精度数。

请注意,代码未经测试,但应该为您提供引导。


Yes, the code you have will copy the pointers, not the doubles. You need to copy the contents of each subarray, and their subarrays (model1, model2, model3 which you don't provide the definitions of. So something like (if model1 etc are just double[]) ...

parameters current;

double checkem[MAX_PARS];
double *p = &checkem[0];

// copy X into checkem
memcpy(p, &current.x, numx * sizeof(struct model1));
p += (numx * sizeof(struct model1)) / sizeof(double);

// copy Y into checkem after X
memcpy(p, &current.y, numy * sizeof(struct model2));
p += (numy * sizeof(struct model2)) / sizeof(double);

// copy Z into checkem after Y
memcpy(p, &current.z, numz * sizeof(struct model3));

If model1, model2 and model3 are more complex than an array of doubles, you'll need to copy the doubles separately.

Note the code is untested, but should give you a steer.

相关问答

更多

如何在c ++中引用动态分配的字符数组(How to reference a dynamically allocated character array in c++)

正如您所理解的那样, items是一个char数组,每个Quack实例都有自己的变量items 。 访问动态分配的数组和静态分配的数组是相同的。 它们分配在连续的内存位置。 char arr[24]; // Allocate 24 bytes in contiguous memory locations in stack char* arr = new char[24]; //Allocate 24 bytes in contiguous memory locations ...

结构数组中的动态分配内存(C中)(Dynamically Allocated Memory in Structure Array (in C))

试试这....我在这里声明一个结构指针tag 。 我为5个学生结构分配内存。 分配值后,现在tag指向最后一个值。所以我将它减少了5次。这个程序只使用一个指针。 如果你想,你可以尝试使用指针数组。 我在程序中提到了更改//changes # include <stdio.h> # include <string.h> # include <stdlib.h> int main (void) { struct student { char initial [21]; int ...

将.txt文件传输到malloc分配的动态内存(Transfer .txt file to dynamic memory allocated by malloc)

由于值从1到9 ,您可以使用0作为每行的标记,这样您就不需要预测或存储每行中的数字,只需存储出现的所有数字,以及最后添加一个0将帮助您知道数字的结束位置,与c处理字符串的方式相同。 以下代码执行我所描述的内容 #include <string.h> #include <stdlib.h> #include <ctype.h> int appendvalue(int ***values, int row, int column, int value) { void *pointer; ...

在动态分配的结构中初始化动态分配的结构(Initialize dynamically allocated structures within dynamically allocated structures)

你没有宣布myDynamicMembersBinArray : struct dynamicbin* myDynamicMembersBinArray = calloc(1, sizeof(struct dynamicbin)); struct bin *binpointer = (struct bin*)calloc(N, sizeof(struct bin)); 应该 (*myDynamicMembersBinArray).binPointer = calloc(N, sizeof(struc...

在VS 2010中调试时如何查看动态分配的数组的内容?(How to see what are the contents of dynamically allocated array while debugging in VS 2010?)

这很简单,你有: char* ptr = new char[10]; 那么如果你在调试器中写: ptr,10 它会向你显示内容,就像它是静态数组一样。 It is quite simple, F.e. you have: char* ptr = new char[10]; Then if you write in debugger: ptr,10 it will show you content as if it were static array.

C - 构建动态分配的指针数组,指向填充了文件输入的结构(C - Build dynamically allocated array of pointers to structures filled with input from file)

我可以给你一些关于如何看待这个问题的片段。 首先 - 我会避免对任何变量使用class名,因为在许多面向对象的编程语言(包括C ++)中,它是一个关键字,不能是变量的名称。 结构DBrecord 使用typedef可能是个好主意。 然后你可以在不使用“struct DBrecord”而只使用“DBrecord”的情况下声明一个struct变量。 但这是可选的。 这是它的样子: typedef struct { int DBrecordID; // ID for each entry c...

在C中删除动态分配的数组成员(Deleting dynamically allocated array members in C)

realloc还可以...但继续阅读:) realloc不会移动部分内存; 它可能会移动整个街区。 因此,您需要在更改分配的大小之前复制数据。 要移动数据, memmove (不是memcpy )是一个不错的选择:它适用于属于同一对象的内存区域。 但要注意不要超过你的数组限制; 就像你在你的代码中一样。 for (i = pos; i < arr_len; i++) { arr[i] = arr[i+1]; } arr[i] = arr[i + 1]; 将尝试访问超过允许大小的一个。 你需要...

交换动态分配的结构(Swapping Dynamic allocated structures)

函数swap()获取两个类型为student **参数。 但是,在呼叫swap(s+3,s+4); ,你传递两个类型为student *参数 - 就像s的类型一样。 你可以编译吗? 无论如何,你在swap()函数中正在做的是替换每个指针指向的内容 。 也就是说,如果你有两个指向学生的指针(例如:p1,指向学生s1,p2指向学生s2),你可以调用swap(&p1, &p2)并让它们指向其他学生(即p1到s2和p2到s1)。 但是在你的main()代码中,你并没有处理指向student的指针。 相反,...

如何将动态分配的双精度结构和子结构的内容传输到数组?(How does one transfer the contents of dynamically allocated structures and substructures of doubles to an array? (C))

是的,您拥有的代码将复制指针,而不是复制指针。 你需要复制每个子model3的内容,以及它们的子model3 ( model3 , model2 , model3 ,你没有提供定义。所以类似的东西(如果model1等只是double [])... parameters current; double checkem[MAX_PARS]; double *p = &checkem[0]; // copy X into checkem memcpy(p, &current.x, numx * si...

从C中的函数返回动态分配的结构数组?(Returning a dynamically allocated array of structures from a function in C?)

你可能想要这个: HashTable *createHashTable(unsigned int size) { //HashTable htable = { 0 }; //return htable; int i; HashTable* htable = NULL; htable = malloc(sizeof(HashTable)* size); for(i=0; i<size; i++) { htable[i].tab...

相关文章

更多

线程报错 thread

/** * 只能这么写 * 不然 腾讯微博会报Can't create handler inside ...

引入thread后socket接受不了报文了

不使用线程thread可以正常接受报文,加了后就在recvfrom那里不动了,是怎么回事? usin ...

关于Thread类中的start()方法和run()方法

引用 public void start() Causes this thread to ...

There is already an open DataReader associated with this Connection which must be closed first

使用MVC4 EF Linq获取foreach列表循环的时候遇到了如下的问题:报错提示 Ther ...

一步一步掌握java的线程机制(二)----Thread的生命周期

之前讲到Thread的创建,那是Thread生命周期的第一步,其后就是通过start()方法来启动Th ...

关于线程

线程,学了挺久了。我发现我不会写总结这种东西。 好吧,我又词穷了。不知道该怎么写了。先打开eclips ...

action 线程问题。

比如现在有个Action,叫CZaction,有很多用户会访问这个action,这个action有对数 ...

多线程问题

请教各位。本人在尝试写一个银行调度系统。但是多线程上遇到问题。目前有三个并发线程,但是同时只能运行两个 ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本